php - 发布到 PHP 脚本中的另一个页面

标签 php post request

如何在 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/

相关文章:

Python 请求。错误 403

node.js - 使用请求时响应中的正文为空

php - 上传到 Heroku 时如何访问 Laravel 符号链接(symbolic link)

javascript - jqgrid自定义JSON数据没有错误但不显示数据

php - 如何使用 php 脚本从 .so 文件调用函数?

java - 如何使用spark java从表单中获取数据?

javascript - 根据所选项目提交表格

javascript - NodeJs - 使用请求模块在 while 循环中有条件地发出 'GET' 请求

python - 无法使用 python 请求登录网站

php - 在 Wordpress 中获取循环外的帖子摘录