使PHP执行POST请求的最简单方法可能是使用cURL,它既可以作为扩展,也可以直接将其外壳化为另一个进程。这是一个帖子示例:
// where are we posting to?$url = 'http://foo.com/script.php';// what post fields?$fields = array( 'field1' => $field1, 'field2' => $field2,);// build the urlenpred data$postvars = http_build_query($fields);// open connection$ch = curl_init();// set the url, number of POST vars, POST datacurl_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 connectioncurl_close($ch);
还要检查Zend框架中的Zend_Http类集,它提供了一个功能强大的HTTP客户端,直接用PHP编写(不需要扩展)。
2014年编辑
-好吧,自从我写那书以来已经有一段时间了。如今,值得检查一下Guzzle,无论是否使用curl扩展,它都可以使用。



