0

我正在使用curl从网站中检索内容,但 curl 检索到的 http 代码为 200 并且内容为空。当我在 Firefox 上使用它时,我看到了 302 重定向。我已经添加了这一行:

curl_setopt($http, CURLOPT_FOLLOWLOCATION, true);

当我使用命令行时,我得到了相同的结果:

curl -I -L http://www.caudalie.fr

在 Firefox 中,最终位置将是http://fr.caudalie.com/但 curl 永远不会得到这个。你有想法吗?

4

1 回答 1

1

我尝试了一些不同的请求标头,从 Firefox 发送的标头开始。最小值不起作用:

bf@desktop-bf:~$ telnet www.caudalie.fr 80
Trying 178.16.174.50...
Connected to www.caudalie.com.
Escape character is '^]'.
GET / HTTP/1.1
Host: www.caudalie.fr
Connection: keep-alive

HTTP/1.1 200 OK
Server: nginx
Date: Fri, 26 Apr 2013 07:23:36 GMT
Content-Type: text/html
Connection: keep-alive
Expires: Sat, 26 Jul 1997 05:00:00 GMT
Vary: Accept-Encoding
Content-Length: 0

如果我提供一种语言,我会得到一个重定向:

bf@desktop-bf:~$ telnet www.caudalie.fr 80
Trying 178.16.174.50...
Connected to www.caudalie.com.
Escape character is '^]'.
GET / HTTP/1.1
Host: www.caudalie.fr
Accept-Language: nl,en;q=0.7,en-us;q=0.3

HTTP/1.1 302 Found
Server: nginx
Date: Fri, 26 Apr 2013 07:23:55 GMT
Content-Type: text/html
Connection: keep-alive
Expires: Sat, 26 Jul 1997 05:00:00 GMT
Location: http://fr.caudalie.com/
Vary: Accept-Encoding
Content-Length: 0

所以,添加一个Accept-Language标题,你应该没问题。在 PHP 中,这将是:

curl_setopt($http,CURLOPT_HTTPHEADER,array('Accept-Language: nl,en;q=0.7;en-us;q=0.3'));

另请参见此处:如何通过 curl 调用使用 HTTP 请求发送标头?

于 2013-04-26T07:26:31.600 回答