Found myself needing to be able to query google via a reverse image lookup to find out more about images I have on my server with unknown contents. Found a good question about this here: php Extract Best guess for this image result from google image search?
Tried implementing the methods listed on there, but it seems like these days, google takes your pretty URL and does a 302 redirect to a seemingly randomly generated nonsense URL that takes you to the image search results. I made sure my code had CURLOPT_FOLLOWLOCATION set to 1 to follow, but I still get back the contents of the 302 page. Here's that code:
function fetch_google($terms="sample search",$numpages=1,$user_agent='Mozilla/5.0 (Windows NT 6.1; rv:8.0) Gecko/20100101 Firefox/8.0')
{
$searched="";
for($i=0;$i<=$numpages;$i++)
{
$ch = curl_init();
$url="http://www.google.com/searchbyimage?hl=en&image_url=".urlencode($terms);
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_USERAGENT, $user_agent);
curl_setopt ($ch, CURLOPT_HEADER, 0);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_REFERER, 'http://www.google.com/');
curl_setopt ($ch,CURLOPT_CONNECTTIMEOUT,120);
curl_setopt ($ch,CURLOPT_TIMEOUT,120);
curl_setopt ($ch,CURLOPT_MAXREDIRS,10);
curl_setopt ($ch,CURLOPT_COOKIEFILE,"cookie.txt");
curl_setopt ($ch,CURLOPT_COOKIEJAR,"cookie.txt");
$searched=$searched.curl_exec ($ch);
curl_close ($ch);
}
$xml = new DOMDocument();
@$xml->loadHTML($searched);
return $searched;
}
$content = fetch_google("http://upload.wikimedia.org/wikipedia/commons/thumb/0/0f/Grosser_Panda.JPG/1280px-Grosser_Panda.JPG",1);
echo $content."<br>";
Also tried another implementation to get back just the URL, and then make a second cURL call after to the URL that was returned. Same outcome, 302 page contents returned. Here's the get url part of that code, the part that would give me a URL to pull from:
function get_furl($url)
{
$furl = false;
// First check response headers
$headers = get_headers($url);
// Test for 301 or 302
if(preg_match('/^HTTP\/\d\.\d\s+(301|302)/',$headers[0]))
{
foreach($headers as $value)
{
if(substr(strtolower($value), 0, 9) == "location:")
{
$furl = trim(substr($value, 9, strlen($value)));
}
}
}
// Set final URL
$furl = ($furl) ? $furl : $url;
return $furl;
}
Any ideas greatly appreciated on this!
Tineye has an API you can use for Reverse Image Search.
http://services.tineye.com/TinEyeAPI
Edit: Here is a solution for creating your own image search engine, written in python flask.
https://github.com/realpython/flask-image-search http://www.pyimagesearch.com/2014/12/08/adding-web-interface-image-search-engine-flask/
I know this has nothing to do with Google, but Tineye is better solution than Google in this regard. Maybe Google should buy them and then they will be Google. haha
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With