我在同一目录中的Web服务器上有2个文件:post.php和receive.php
post.php文件发布用户名和密码。 receive.php接收用户名和密码,然后打印出来。
receive.php文件如下所示:
<?php
$user=$_POST["user"];
$password=$_POST["password"];
echo("The Username is : ".$user."<br>");
echo("The Password is : ".$password."<br>");
?>
我有post.php的代码:
<?php
$r = new HttpRequest('http://localhost/receive.php', HttpRequest::METH_POST);
$r->addPostFields(array('user' => 'mike', 'password' => '1234'));
try {
echo $r->send()->getBody();
} catch (HttpException $ex) {
echo $ex;
}
?>
我尝试了各种不同的编码post.php文件的方法,但都没有。我也尝试过在线学习一些教程,但这也没有用。我是一个PHP菜鸟,请帮助!!
答案 0 :(得分:3)
答案 1 :(得分:3)
post.php
的以下代码对我有用。我不是100%确定它做了什么,但它确实有效。
<?php
$params = array ('user' => 'Mike', 'password' => '1234');
$query = http_build_query ($params);
// Create Http context details
$contextData = array (
'method' => 'POST',
'header' => "Connection: close\r\n".
"Content-Length: ".strlen($query)."\r\n",
'content'=> $query );
// Create context resource for our request
$context = stream_context_create (array ( 'http' => $contextData ));
// Read page rendered as result of your POST request
$result = file_get_contents (
'http://localhost/receive.php', // page url
false,
$context);
// Server response is now stored in $result variable so you can process it
echo($result);
?>