PHP Search By Image Google cURL return 302 moved PHP Search By Image Google cURL return 302 moved curl curl

PHP Search By Image Google cURL return 302 moved


It can happen that following redirection is blocked for your curl at your server. So I'll recommend you to handle the redirection manually. Like this one:

First your curl function. You can add other curl options if you like:

function curl($url, $user_agent, $retry=0){    if($retry > 5){        print "Maximum 5 retries are done, skipping!\n";        return "in loop!";    }    $ch = curl_init();    curl_setopt ($ch, CURLOPT_URL, $url);    curl_setopt ($ch, CURLOPT_USERAGENT, $user_agent);    curl_setopt ($ch, CURLOPT_HEADER, TRUE);    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, TRUE);    curl_setopt ($ch, CURLOPT_REFERER, 'http://www.google.com/');    curl_setopt ($ch, CURLOPT_COOKIEFILE,"./cookie.txt");    curl_setopt ($ch, CURLOPT_COOKIEJAR,"./cookie.txt");    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);    $result = curl_exec($ch);    curl_close($ch);    // handling the follow redirect    if(preg_match("|Location: (https?://\S+)|", $result, $m)){        print "Manually doing follow redirect!\n$m[1]\n";        return curl($m[1], $user_agent, $retry + 1);    }    // add another condition here if the location is like Location: /home/products/index.php    return $result;}

And here is how it should be called:

$response = curl("http://www.google.com/", "Mozilla 5.0");print "$response\n";

I am parsing the follow link from the Location: header. It can happen that the link is not started with http:// That case add another condition over there.


The problem is probably with CURLOPT_FOLLOWLOCATION which is unavailable if safe_mode or open_basedir are enabled in php.ini

Try this answer: http://au.php.net/manual/ro/function.curl-setopt.php#71313

Just replace curl_exec() with curl_redir_exec() provided in the comment.


Check if you didn't hit max request per IP. You may be redirected to captcha page. AFAIK it is against google rules to use a robot to query google.