可能最简单的方法使PHP执行POST请求是使用
cURL,作为一个
extension或简单地shelling到另一个进程。这里是一个帖子样例:
// 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 EDIT – 好吧,自从我写了一段时间。这些天值得检查Guzzle,它可以再次使用或不使用curl扩展。