使用 fopen 的 Http post 请求
Http post request using fopen
在第一个文件——我执行的文件中,我有以下内容:
<?php
$name = "Julia";
$article = "I like papers";
$url = "http://domain.com/process.php";
$param = array('http' => array(
'method' => 'POST',
'content' => $article
));
$mad = @stream_context_create($param);
$fp = @fopen($url, 'rb', false, $mad);
$response = @stream_get_contents($fp);
echo $response;
?>
在第二个文件中 http://domain.com/process.php 我有这个:
<?php
$name = $_POST["name"];
$article = $_POST["content"];
$article = $_POST["article"];
echo $article;
echo $name;
echo "Hello there</br>:\n";
?>
我得到的输出是:
"Hello there"
怎么了,我如何通过请求传递值 $article 和 $name 以及如何将它们提取到文件中 process.php?
您在内容部分弄错了,请使用 http_build_query()
构建 POST 查询。
$param = array( 'http' => array('method' => 'POST',
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'content' => http_build_query($data)
));
而您的 post 参数应该如下所示,其中输入名称作为键
$data['name'] = 'Julia';
$data['article'] = 'I like papers';
就我个人而言,我会使用 curl。
在第一个文件——我执行的文件中,我有以下内容:
<?php
$name = "Julia";
$article = "I like papers";
$url = "http://domain.com/process.php";
$param = array('http' => array(
'method' => 'POST',
'content' => $article
));
$mad = @stream_context_create($param);
$fp = @fopen($url, 'rb', false, $mad);
$response = @stream_get_contents($fp);
echo $response;
?>
在第二个文件中 http://domain.com/process.php 我有这个:
<?php
$name = $_POST["name"];
$article = $_POST["content"];
$article = $_POST["article"];
echo $article;
echo $name;
echo "Hello there</br>:\n";
?>
我得到的输出是:
"Hello there"
怎么了,我如何通过请求传递值 $article 和 $name 以及如何将它们提取到文件中 process.php?
您在内容部分弄错了,请使用 http_build_query()
构建 POST 查询。
$param = array( 'http' => array('method' => 'POST',
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'content' => http_build_query($data)
));
而您的 post 参数应该如下所示,其中输入名称作为键
$data['name'] = 'Julia';
$data['article'] = 'I like papers';
就我个人而言,我会使用 curl。