2013-06-26 80 views
0

我有以下脚本:PHP卷曲不设置头

$curl = curl_init(); 
curl_setopt($curl,CURLOPT_RETURNTRANSFER,true); 
curl_setopt($curl, CURLOPT_URL, 'http://4pda.ru/forum/index.php?act=idx'); 
curl_setopt($curl, CURLOPT_HEADER, true); 

$headers = array(); 
$headers[] = 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'; 
$headers[] = 'Connection: Keep-Alive'; 
$headers[] = 'Accept-Language: ru-RU,ru;q=0.8,en-US;q=0.5,en;q=0.3'; 
$headers[] = 'Accept-Encoding: gzip, deflate'; 
$headers[] = 'Host: 4pda.ru'; 
$headers[] = 'User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:22.0) Gecko/20100101 Firefox/22.0'; 


curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); 
$cookie_path = "./cookies.txt"; 
curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie_path); 
curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie_path); 
curl_setopt($curl, CURLINFO_HEADER_OUT, true); 

$result = curl_exec($curl); 
var_dump(curl_getinfo($curl,CURLINFO_HEADER_OUT)); //problem one 

curl_close($curl); 
var_dump($result); 

我有两个问题。首先,PHP Curl不设置标题。它只发送这些头文件:

User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:22.0) Gecko/20100101 Firefox/22.0 
Host: 4pda.ru 
Accept: */* 

你可以看到php设置不正确。 第二个问题是,服务器返回以下内容:

HTTP/1.1 403 Forbidden 
Server: nginx 
Date: Wed, 26 Jun 2013 14:26:18 GMT 
Content-Type: text/html 
Content-Length: 162 
Connection: keep-alive 
Keep-Alive: timeout=20 

<html> 
<head><title>403 Forbidden</title></head> 
<body bgcolor="white"> 
<center><h1>403 Forbidden</h1></center> 
<hr><center>nginx</center> 
</body> 
</html> 

虽然,我期望能获得页面的HTML。

令人惊讶的是,这个脚本在我的家庭服务器(Windows)和我以前的服务器上正常工作。我认为我的问题是由于错误地配置了新的服务器。但我可能是错的。

我很乐意提供任何建议。 问候,丹尼斯。

更新。谢谢马川,第一个问题就解决了。

回答

2

难道你没有在该设置的标题行中使用错误的变量名吗?

curl_setopt($curlHandle, CURLOPT_HTTPHEADER, $headers); 

应该读

curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); 
+0

谢谢你什么你已经找到了我的错字。第一个问题解决了。不幸的是,第二个问题是紧迫的。 – Denis

+0

在您的请求头示例中,我看不到cookie标头。服务器是否需要发送Cookie进行授权? –

+0

不,你可以去http://4pda.ru/forum/index.php?act=idx获取没有cookies的内容。 – Denis