卷曲不会得到302而是200

时间:2013-04-26 07:14:57

标签: php curl

我正在使用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永远不会得到这个。 你知道吗?

1 个答案:

答案 0 :(得分: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'));

另见:How to send a header using a HTTP request through a curl call?