0

Even though I have set curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true) cURL doesn't want to follow redirects, it only shows the "301 Moved page". Tried it with multiple sites. Strange thing is that it works on localhost, but when I upload it to my webspace then refuses to work.

Is it possible that my web hosting provider made some tweaks that it doesn't work? Never seen such thing :(

Here's the code:

    $ch=curl_init();
    curl_setopt($ch, CURLOPT_URL, 'http://google.com');
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_ENCODING, '');
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36');
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
        'Accept-Language: en-US,en;q=0.5',
        'Accept-Encoding: gzip, deflate',
        'Connection: keep-alive'
        ));
    $result = curl_exec($ch);
    curl_close($ch);
ss1
  • 1,009
  • 15
  • 32
  • 1
    `CURLOPT_FOLLOWLOCATION` cannot be activated when `safe_mode` is enabled or an `open_basedir` is set. Does your server have either of those enabled/set? – showdev Jan 09 '15 at 18:11
  • @showdev Thanks, Forgot to turn on error reporting, got the same result. If you want you can put this in an answer and I will accept it. – ss1 Jan 09 '15 at 18:24
  • 1
    possible duplicate of [CURLOPT\_FOLLOWLOCATION cannot be activated](http://stackoverflow.com/questions/6352927/curlopt-followlocation-cannot-be-activated) – showdev Jan 09 '15 at 18:30

1 Answers1

0

I had a similar issue and it was due to cURL executing a GET immediately after receiving the redirect header. To fix this i specified CURLOPT_CUSTOMREQUEST

Example:

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");

Kevin Lynch
  • 24,427
  • 3
  • 36
  • 37