fopen 不工作:为什么 $_POST 是空的?
fopen not working : why is $_POST empty?
我有两个文件 active.php 和 passive.php。
如前所述,我希望 active.php 到 post 数据无需用户交互到 passive.php
here。
如果我的代码有效,最后 $_POST 数组应该包含 'Name'=>'John Doe'
正如我的导航员告诉我的那样是空的。我做错了什么?
我完全知道有使用 Javascript 的解决方案(如 here ) or cURL (as explained here 所述),但这个问题专门针对 PHP-only 方法,所以请不要
post 任何涉及 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);
?>
在 PHP 手册页上 found here 评价最高的评论解释了如何使用流执行 POST 操作。正如上面评论中提到的,你只是错过了一个 header
$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);
我有两个文件 active.php 和 passive.php。 如前所述,我希望 active.php 到 post 数据无需用户交互到 passive.php here。 如果我的代码有效,最后 $_POST 数组应该包含 'Name'=>'John Doe' 正如我的导航员告诉我的那样是空的。我做错了什么?
我完全知道有使用 Javascript 的解决方案(如 here ) or cURL (as explained here 所述),但这个问题专门针对 PHP-only 方法,所以请不要 post 任何涉及 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);
?>
在 PHP 手册页上 found here 评价最高的评论解释了如何使用流执行 POST 操作。正如上面评论中提到的,你只是错过了一个 header
$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);