如何在 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_Http Zend 框架中的一组类,它提供了一个功能强大的 HTTP 客户端,直接用 PHP 编写(无需扩展)。
2014 年编辑 - 好吧,我已经有一段时间没写那篇文章了。这些天值得检查Guzzle无论有没有 curl 扩展,它都可以工作。
关于php - 发布到 PHP 脚本中的另一个页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1217824/