2012-12-05 18 views
1

下面的代码在本地主机和服务器上给出了不同的输出。 我试着改变useragents和浏览器。我是否错过任何标题?在localhost为什么cURL在服务器和本地主机上提供不同的输出?

<?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

我想输出作为其在本地主机上。

+0

您正在做任何发布数据的请求? – Ranty

+0

不行我不是在做后期要求。 – virpara

+0

从你已经链接的页面:'请求标题POST /login.php HTTP/1.1' – Ranty

回答

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的回报。

相关问题