1

下面的代码在 localhost 和服务器上给出了不同的输出。我尝试更改用户代理和浏览器。我错过了任何标题吗?

<?php

$headers[] = 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';
$headers[] = 'Connection: Keep-Alive';
$headers[] = 'Content-type: application/x-www-form-urlencoded;charset=UTF-8';
$headers[] = 'Accept-Language: en-us,en;q=0.5';
$headers[] = 'Accept-Encoding   gzip,deflate';
$headers[] = 'Keep-Alive: 300';
$headers[] = 'Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7';
//$headers[] = 'Content-Length: 0';

$curl = curl_init();
curl_setopt($curl, CURLOPT_HEADERFUNCTION, 'dbg_curl_data');
curl_setopt($curl, CURLOPT_WRITEFUNCTION, 'dbg_curl_data');
curl_setopt($curl, CURLINFO_HEADER_OUT, true);
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.0; en; rv:1.9.1.7) Gecko/20091221 Firefox/3.5.7');
//curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11');
curl_setopt($curl, CURLOPT_URL, "http://www.facebook.com/login.php");
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_ENCODING, 'gzip');
//curl_setopt($curl, CURLOPT_HEADER, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_VERBOSE, 1);
//curl_setopt($curl, CURLOPT_POST, true);

$html = curl_exec ($curl);
//curl_close ($curl);

echo '<fieldset><legend>request headers</legend>
<pre>', htmlspecialchars(curl_getinfo($curl, CURLINFO_HEADER_OUT)), '</pre>
</fieldset>';

echo '<fieldset><legend>response</legend>
<pre>', htmlspecialchars(dbg_curl_data(null)), '</pre>
</fieldset>';

echo '<fieldset><legend>$html</legend>
<pre>', htmlspecialchars($html), '</pre>
</fieldset>';

function dbg_curl_data($curl, $data=null) {
 static $buffer = '';

 if ( is_null($curl) ) {
   $r = $buffer;
   $buffer = '';
   return $r;
 }
 else {
   $buffer .= $data;
   return strlen($data);
 }
}
?>

本地主机上的输出:http: //pastebin.com/UbpTVc4f

服务器上的输出:http: //pastebin.com/NURDksJy

我想在本地主机上输出。

4

1 回答 1

0

为什么不先从简单的事情开始,然后再像现在这样复杂化您的请求并且难以调试。例如,这段代码(在单独的文件中运行):

$curl = curl_init();
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.0; en; rv:1.9.1.7) Gecko/20091221 Firefox/3.5.7');
curl_setopt($curl, CURLOPT_URL, "http://www.facebook.com/login.php");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HEADER, 1);
$html = curl_exec ($curl);
print_r($html);

应该给你HTTP/1.1 302 Found回报。

于 2012-12-05T11:43:25.357 回答