Question

I have an error curl code 301. I get an error 301 when I made ​​the request to curl leboncoin.fr I try to solve the problem by adding: curl_setopt ($ ch, CURLOPT_FOLLOWLOCATION, 1) in the code of my function curl. Code work find on one day only. and next day I found again the same code erreor(301 error)

Here are the curl code below:

function file_get_contents_curl($url)
{
        $ch = curl_init();
        $timeout = 10;

        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
        curl_setopt($ch, CURLOPT_URL, $url);
        **curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);**
        curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER , 1);
        curl_setopt($ch, CURLOPT_FORBID_REUSE , 1);
        curl_setopt($ch, CURLOPT_TIMEOUT  , 10);
        curl_setopt( $ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3)    Gecko/20041001 Firefox/0.10.1" );
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        $data = curl_exec($ch);
        curl_close($ch);
         return $data;
}

do you have any idea to solve this? thanks.

Was it helpful?

Solution

If it works one day and there are no changes on your side or the site you are trying to connect, it should be the same result.

In any case, since you know that the address changed, change it in your code to reduce time and steps.

Also, you may be having problems due to the time limit set for the wait on connection, try to increase CURLOPT_CONNECTTIMEOUT a bit, like 13, just in case the servers are taking too long to respond or do the redirection.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top