我有两个文件active.php和passive.php。 我希望active.php在没有用户交互的情况下发布数据到passive.php,如上所述 here。 如果我的代码有效,那么$ _POST数组最后应该包含'Name'=>'John Doe' 因为我的导航员通知我是空的。我做错了什么?
我完全清楚有些解决方案使用Javascript(如here解释)或cURL(解释为here),但这个问题是关于PHP专用方法的,所以请做不 发布任何涉及Javascript或cURL的评论或答案。
passive.php的内容:
<?php
var_dump($_POST);
?>
active.php的内容:
<?php
$post_data=array('Name'=>'John Doe');
$url='http://localhost:8888/passive.php';
$params = array('http' => array(
'method' => 'POST',
'content' => $post_data
));
$ctx = stream_context_create($params);
$fp = @fopen($url, 'rb', false, $ctx);
$response = @stream_get_contents($fp);
fclose($fp);
var_dump($response);
?>
答案 0 :(得分:0)
在PHP手册页本身found here上,评分最高的评论解释了如何使用流执行POST操作。正如上面的评论所述,你只是缺少一个标题
$post_data=array('Name'=>'John Doe');
$url='http://localhost:8888/passive.php';
$params = array(
'http' => array
(
'method' => 'POST',
'header'=>"Content-Type: application/x-www-form-urlencoded",
'content' => $post_data
)
);
$ctx = stream_context_create($params);
$fp = fopen($url, 'rb', false, $ctx);
$response = stream_get_contents($fp);