0

我有以下脚本:

$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 设置的 headers 不正确。第二个问题是服务器返回如下内容:

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)和我以前的服务器上运行良好。我认为我的问题是由于错误地配置了新服务器。但我可能是错的。

我很乐意接受任何建议。问候,丹尼斯。

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

4

1 回答 1

2

您没有在该集合标题行中使用错误的变量名吗?

curl_setopt($curlHandle, CURLOPT_HTTPHEADER, $headers);

应该读

curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
于 2013-06-26T15:58:05.173 回答