title: Google reverse image lookup in PHP
body: <p>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: <a href="https://stackoverflow.com/questions/10634801/php-extract-best-guess-for-this-image-result-from-google-image-search">php Extract Best guess for this image result from google image search?</a></p>
  
  <p>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:
      
  
  <pre><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&lt;=$numpages;$i++)
      {
          $ch = curl_init();
          $url="http://www.google.com/searchbyimage?hl=en&amp;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-&gt;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."&lt;br&gt;";
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>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;
  }
  </code></pre>
  
  <p>Any ideas greatly appreciated on this!</p>