0

I am using curl to retrieve content from a website, but the http code retrieved by curl is 200 and the content is empty. When I used this on firefox, I see a 302 redirection. I have already addes this line:

curl_setopt($http, CURLOPT_FOLLOWLOCATION, true);

When I used the command line, I get the same result:

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

In Firefox, the final location will be http://fr.caudalie.com/ but curl never gets this. Have you an idea?

Till Helge
  • 9,253
  • 2
  • 40
  • 56

1 Answers1

1

I tried some different request headers, starting from the headers Firefox sent. The minimum doesnt work:

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

I got a redirect if I give a language:

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

So, add a Accept-Language header and you should be OK. In PHP, that would be:

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

See also here: How to send a header using a HTTP request through a curl call?

Community
  • 1
  • 1
Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195