如何使用PHP header()函数POST到页面?

我在这里find了下面的代码,我认为这是我想要的,但是它不起作用:

$host = "www.example.com"; $path = "/path/to/script.php"; $data = "data1=value1&data2=value2"; $data = urlencode($data); header("POST $path HTTP/1.1\r\n"); header("Host: $host\r\n"); header("Content-type: application/x-www-form-urlencoded\r\n"); header("Content-length: " . strlen($data) . "\r\n"); header("Connection: close\r\n\r\n"); header($data); 

我正在寻找张贴表单数据,而不用发送用户到中间页面,然后使用JavaScript来redirect他们。 我也不想使用GET,所以使用后退button并不容易。

这段代码有什么问题吗? 还是有更好的方法?

编辑我正在考虑头function会做什么。 我以为我可以让浏览器回传数据到服务器,但这不是它的意思。 相反,我在我的代码中find了一种方法,以避免需要一个职位(不打破,只是继续在交换机内的下一个案例)。

头函数用于将HTTP响应头发回给用户(即不能用它来创build请求头。

请问你为什么这样做? 为什么模拟一个POST请求时,你可以在那里,然后对数据进行处理? 我假设当然script.php驻留在您的服务器上。

要创build一个POST请求,使用fsockopen()打开一个到主机的TCP连接,然后在fsockopen()返回的处理函数上使用fwrite(),这个函数与在OP中的头函数中使用的值相同。 或者,您可以使用cURL。

今天非常需要这个答案,因为不是每个人都想使用cURL来使用Web服务。 此外PHP也允许使用下面的代码

 function get_info() { $post_data = array( 'test' => 'foobar', 'okay' => 'yes', 'number' => 2 ); // Send a request to example.com $result = $this->post_request('http://www.example.com/', $post_data); if ($result['status'] == 'ok'){ // Print headers echo $result['header']; echo '<hr />'; // print the result of the whole request: echo $result['content']; } else { echo 'A error occured: ' . $result['error']; } } function post_request($url, $data, $referer='') { // Convert the data array into URL Parameters like a=b&foo=bar etc. $data = http_build_query($data); // parse the given URL $url = parse_url($url); if ($url['scheme'] != 'http') { die('Error: Only HTTP request are supported !'); } // extract host and path: $host = $url['host']; $path = $url['path']; // open a socket connection on port 80 - timeout: 30 sec $fp = fsockopen($host, 80, $errno, $errstr, 30); if ($fp){ // send the request headers: fputs($fp, "POST $path HTTP/1.1\r\n"); fputs($fp, "Host: $host\r\n"); if ($referer != '') fputs($fp, "Referer: $referer\r\n"); fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n"); fputs($fp, "Content-length: ". strlen($data) ."\r\n"); fputs($fp, "Connection: close\r\n\r\n"); fputs($fp, $data); $result = ''; while(!feof($fp)) { // receive the results of the request $result .= fgets($fp, 128); } } else { return array( 'status' => 'err', 'error' => "$errstr ($errno)" ); } // close the socket connection: fclose($fp); // split the result header from the content $result = explode("\r\n\r\n", $result, 2); $header = isset($result[0]) ? $result[0] : ''; $content = isset($result[1]) ? $result[1] : ''; // return as structured array: return array( 'status' => 'ok', 'header' => $header, 'content' => $content); } 

除了Salaryman说的外,还要看一下PEAR中的类,那里有一些HTTP请求类,即使你没有在你的PHP发行版中安装cURL扩展,也可以使用。

有一个很好的class级可以做你想做的事情。 可以从http://sourceforge.net/projects/snoopy/下载;

 private function sendHttpRequest($host, $path, $query, $port=80){ header("POST $path HTTP/1.1\r\n" ); header("Host: $host\r\n" ); header("Content-type: application/x-www-form-urlencoded\r\n" ); header("Content-length: " . strlen($query) . "\r\n" ); header("Connection: close\r\n\r\n" ); header($query); } 

这会让你马上