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

问题描述:

如何在php脚本中向另一个php页面发出发布请求?我有一台前端计算机作为html页面服务器,但是当用户单击一个按钮时,我希望后端服务器进行处理,然后将信息发送回前端服务器以显示给用户.我说的是我可以在后端计算机上有一个php页面,它将信息发送回前端.因此,再一次,如何从一个php页面向另一个php页面发出POST请求?

How can I make a post request to a different php page within a php script? I have one front end computer as the html page server, but when the user clicks a button, I want a backend server to do the processing and then send the information back to the front end server to show the user. I was saying that I can have a php page on the back end computer and it will send the information back to the front end. So once again, how can I do a POST request to another php page, from a php page?

使PHP执行POST请求的最简单方法可能是使用扩展名,也可以直接扩展到另一个进程.这是一个帖子示例:

Possibly the easiest way to make PHP perform a POST request is to use cURL, either as an extension or simply shelling out to another process. Here's a post sample:

// 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编写(无需扩展).

Also check out Zend_Http set of classes in the Zend framework, which provides a pretty capable HTTP client written directly in PHP (no extensions required).

2014编辑-嗯,距离我写这篇文章已有一段时间了.这些天,值得检查 Guzzle ,无论是否使用curl扩展,它都可以使用.

2014 EDIT - well, it's been a while since I wrote that. These days it's worth checking Guzzle which again can work with or without the curl extension.