为什么cURL在服务器和localhost上提供不同的输出?

时间:2012-12-05 10:49:37

标签: php curl

以下代码在localhost和server上提供不同的输出。 我尝试改变用户和浏览器。我错过任何标题吗?

<?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);
 }
}
?>

Localhost上的输出:http://pastebin.com/UbpTVc4f

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

我希望输出为localhost。

1 个答案:

答案 0 :(得分: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作为回报。