0

I'm using php 5.5.8 and I've got this curl function:

function getBody($url){
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_HEADER, false);
    curl_setopt($curl, CURLOPT_NOBODY, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_TIMEOUT,120);
    curl_setopt($curl, CURLOPT_CUSTOMREQUEST,"GET");
    curl_setopt ($curl, CURLOPT_FOLLOWLOCATION, true );
    curl_setopt ($curl, CURLOPT_ENCODING, "" );
    curl_setopt($curl, CURLOPT_VERBOSE, 1);
    curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1a2pre) Gecko/2008073000 Shredder/3.0a2pre ThunderBrowse/3.2.1.8"); 
    $data = curl_exec($curl);
    $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
    if($httpCode == 404) {
        return "-1";
    }
    curl_close($curl);
    return $data;
}

These two are supposed to prevent this but i still get 301 error

 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

And my safe_mode = off;

I looked for the answer but none of them helped me with this problem

Any suggestions ?

Thank you :)

Mike
  • 23,542
  • 14
  • 76
  • 87
SirMissAlot
  • 120
  • 2
  • 11
  • 1
    Hello! A 301 error is "Moved Permanently". Are you sure your URL is correct? – tjfo Jun 01 '16 at 14:34
  • 301 isn't an error. it's a "go over there to get the content". and note that your customrequest is redundant. `get` is the default for curl – Marc B Jun 01 '16 at 14:38
  • if the url is correct, the server is telling you that the resource has been moved permanently. Try to follow redirects with your cURL request. – YvesLeBorg Jun 01 '16 at 14:40
  • 1
    @YvesLeBorg OP has `curl_setopt ($curl, CURLOPT_FOLLOWLOCATION, true );` in the code. That should cause it to follow the redirects. – Mike Jun 01 '16 at 14:41
  • @Mike ... duh , time for coooofee , never saw that, was looking for a maxredirs directive :) – YvesLeBorg Jun 01 '16 at 14:46
  • @OP: I executed your code and it worked fine for me. You need to create an [MCVE](http://stackoverflow.com/help/mcve). – Mike Jun 01 '16 at 14:50
  • Also note that safe mode has been removed from PHP since 5.4 and will have no effect if you're using PHP >= 5.4. – Mike Jun 01 '16 at 15:02

0 Answers0