2013-04-26 33 views
0

我使用curl從網站檢索內容,但curl檢索到的http代碼是200,內容是空的。當我在firefox上使用這個時,我看到了302重定向。我已經就將此這一行:curl沒有得到302但是200

curl_setopt($http, CURLOPT_FOLLOWLOCATION, true); 

當我使用的命令行,我得到了相同的結果:

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

在Firefox中,最後的位置將是http://fr.caudalie.com/但捲曲從未得到這一點。 你有想法嗎?

+0

我該地址獲得了401。 – 2013-04-26 07:16:44

+0

顯示您的完整捲曲php代碼 – shapeshifter 2013-04-26 07:18:23

+0

也可能不是標題重定向? http://curl.haxx.se/docs/faq.html#Redirects_work_in_browser_but_no – shapeshifter 2013-04-26 07:20:21

回答

1

我嘗試了一些不同的請求標題,從Firefox發送的標題開始。最小不工作:

[email protected]:~$ 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 

我有一個重定向,如果我給語言:

[email protected]:~$ 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?

+0

謝謝,但它不起作用。任何其他想法? – 2013-04-26 12:18:14

+0

我希望你沒有複製/粘貼我的PHP代碼,因爲它有錯誤。 – 2013-04-26 13:10:01

+0

我只採取了最後一行,我已經正確的http與我的變量。 – 2013-04-26 13:37:32