【发布时间】:2010-11-16 02:41:50
【问题描述】:
如何在 php 脚本中向不同的 php 页面发出 post 请求?我有一台前端计算机作为 html 页面服务器,但是当用户单击按钮时,我希望后端服务器进行处理,然后将信息发送回前端服务器以显示用户。我是说我可以在后端计算机上有一个 php 页面,它将信息发送回前端。那么再一次,我怎样才能从一个 php 页面向另一个 php 页面发出 POST 请求?
【问题讨论】:
如何在 php 脚本中向不同的 php 页面发出 post 请求?我有一台前端计算机作为 html 页面服务器,但是当用户单击按钮时,我希望后端服务器进行处理,然后将信息发送回前端服务器以显示用户。我是说我可以在后端计算机上有一个 php 页面,它将信息发送回前端。那么再一次,我怎样才能从一个 php 页面向另一个 php 页面发出 POST 请求?
【问题讨论】:
让 PHP 执行 POST 请求的最简单的方法可能是使用cURL,或者作为extension,或者只是将其发送给另一个进程。这是一个帖子示例:
// where are we posting to?
$url = 'http://foo.com/script.php';
// what post fields?
$fields = array(
'field1' => $field1,
'field2' => $field2,
);
// build the urlencoded data
$postvars = http_build_query($fields);
// open connection
$ch = curl_init();
// set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $postvars);
// execute post
$result = curl_exec($ch);
// close connection
curl_close($ch);
还可以查看 Zend 框架中的 Zend_Http 类集,它提供了一个非常强大的 HTTP 客户端,直接用 PHP 编写(无需扩展)。
2014 年编辑 - 好吧,我已经有一段时间没有写了。这些天值得检查Guzzle 再次可以使用或不使用 curl 扩展。
【讨论】:
假设您的 php 安装具有 CURL 扩展名,这可能是最简单的方法(如果您愿意,也是最完整的)。
示例 sn-p:
//set POST variables
$url = 'http://domain.com/get-post.php';
$fields = array(
'lname'=>urlencode($last_name),
'fname'=>urlencode($first_name),
'email'=>urlencode($email)
);
//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
学分转到http://php.dzone.com。 另外,不要忘记访问 PHP 手册中相应的page(s)
【讨论】:
对于 PHP 处理,请查看cURL。它将允许您在后端调用页面并从中检索数据。基本上你会做这样的事情:
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_URL,$fetch_url);
curl_setopt ($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt ($ch,CURLOPT_USERAGENT, $user_agent;
curl_setopt ($ch,CURLOPT_CONNECTTIMEOUT,60);
$response = curl_exec ( $ch );
curl_close($ch);
您也可以查看PHP HTTP Extension。
【讨论】:
正如其他用户所说,使用 CURL 最容易做到这一点。
如果 curl 不适合您,那么也许 http://netevil.org/blog/2006/nov/http-post-from-php-without-curl
如果不可能,您可以自己编写套接字 http://petewarden.typepad.com/searchbrowser/2008/06/how-to-post-an.html
【讨论】:
index.php
$url = 'http://[host]/test.php';
$json = json_encode(['name' => 'Jhonn', 'phone' => '128000000000']);
$options = ['http' => [
'method' => 'POST',
'header' => 'Content-type:application/json',
'content' => $json
]];
$context = stream_context_create($options);
$response = file_get_contents($url, false, $context);
test.php
$raw = file_get_contents('php://input');
$data = json_decode($raw, true);
echo $data['name']; // Jhonn
【讨论】:
对于使用 cURL 的用户,请注意 CURLOPT_POST 选项被视为布尔值,因此实际上无需将其设置为您要发布的字段数。
将 CURLOPT_POST 设置为 TRUE(即除零以外的任何整数)只会告诉 cURL 将数据编码为 application/x-www-form-urlencoded,尽管我敢打赌,当您将 urlencoded 字符串作为 CURLOPT_POSTFIELDS 传递时,这并不是绝对必要的,因为cURL 应该已经通过后一个选项设置的值的类型(字符串与数组)来告诉编码。
另请注意,从 PHP 5 开始,您可以使用 http_build_query 函数让 PHP 为您的 fields 数组进行 urlencode,如下所示:
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));
【讨论】:
解决方案在 target="_blank" 中,如下所示:
http://www.ozzu.com/website-design-forum/multiple-form-submit-actions-t25024.html
像这样编辑表格:
<form method="post" action="../booking/step1.php" onsubmit="doubleSubmit(this)">
并使用这个脚本:
<script type="text/javascript">
<!--
function doubleSubmit(f)
{
// submit to action in form
f.submit();
// set second action and submit
f.target="_blank";
f.action="../booking/vytvor.php";
f.submit();
return false;
}
//-->
</script>
【讨论】:
虽然不理想,但如果 cURL 选项不适合您,可以尝试使用 shell_exec();
【讨论】:
CURL 方法非常流行,所以使用它很好。你也可以用一些额外的 cmets 来解释更多这些代码,因为初学者可以理解它们。
【讨论】: