title: PHP Search By Image Google cURL return 302 moved
body: <p>I'm trying to get a simple string with the description of the image I searched with <a href="https://github.com/hbattat/search-by-image" rel="nofollow noreferrer">search-by-image</a>. So I set up my search_by_google.php page:</p>
  
  <pre><code>    &lt;?php
  $url = $_REQUEST['url'];
  
  if(empty($_REQUEST['raw'])){
  $raw = false;
  }
  else{
  $raw = true;
  }
  echo fetch_google($url, $raw);
  
  function fetch_google($u, $raw, $terms="sample search",$numpages=1,$user_agent='Mozilla/5.0 (Windows NT 6.1; rv:8.0) Gecko/20100101 Firefox/8.0')
  {
  
      $ch = curl_init();
      $url = 'http://www.google.com/imghp?hl=en&amp;tab=wi';
      curl_setopt ($ch, CURLOPT_URL, $url);
      curl_setopt ($ch, CURLOPT_USERAGENT, $user_agent);
      curl_setopt ($ch, CURLOPT_HEADER, TRUE);
      curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, TRUE);
      curl_setopt ($ch, CURLOPT_RETURNTRANSFER, TRUE);
      curl_setopt ($ch, CURLOPT_VERBOSE,true);
      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");
      curl_setopt ($ch, CURLOPT_VERBOSE,true);
      curl_exec($ch);
  
  $searched="";
  for($i=0;$i&lt;=$numpages;$i++)
  {
      $ch = curl_init();
      $url="http://www.google.com/searchbyimage?hl=en&amp;image_url=".urlencode($u);
      curl_setopt ($ch, CURLOPT_URL, $url);
      curl_setopt ($ch, CURLOPT_USERAGENT, $user_agent);
      curl_setopt ($ch, CURLOPT_HEADER, TRUE);
      curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, TRUE);
      curl_setopt ($ch, CURLOPT_RETURNTRANSFER, TRUE);
      curl_setopt ($ch, CURLOPT_VERBOSE,true);
      curl_setopt ($ch, CURLOPT_REFERER, 'http://www.google.com/imghp?hl=en&amp;tab=wi');
      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);
  }
  if($raw){
          return $searched;
      }
      else{
          $matches = array();
          preg_match('/Best guess for this image:[^&lt;]+&lt;a[^&gt;]+&gt;([^&lt;]+)/', $searched, $matches);
          return (count($matches) &gt; 1 ? $matches[1] : false);
      }
   }
    ?&gt;
  </code></pre>
  
  <p>I've changed all the curl options but if I go to <a href="http://www.mysite.altervista.org/search_by_google.php?url=http://www.mysite.org/asdasd.jpg&amp;raw=false" rel="nofollow noreferrer">http://www.mysite.altervista.org/search_by_google.php?url=http://www.mysite.org/asdasd.jpg&amp;raw=false</a></p>
  
  <p>It keep me saying 302 Moved</p>
  
  <p><img src="https://i.stack.imgur.com/QtMqT.png" alt="enter image description here"></p>
  
  <p>I have changed my code putting </p>
  
  <pre><code>    curl_setopt ($ch, CURLOPT_HEADER, TRUE);
  </code></pre>
  
  <p>in the second curl_init() and now it gives me this message:</p>
  
  <p><img src="https://i.stack.imgur.com/C4R7w.png" alt="enter image description here"></p>
  
  <p><strong><em>EDIT 25/03/2014 19:34</em></strong></p>
  
  <p>I changed my code like Sabuj Hassan said and the log now is:</p>
  
  <pre><code>HTTP/1.0 302 Found Cache-Control: public, max-age=21600 Date: Tue, 25 Mar 2014 18:30:07 GMT Age: 16 Location: http://www.google.com/search?tbs=sbi:AMhZZisAo2ZcfY19aFUJcEj26M4zKc9ZuxzfsUPzLuUJk-pd-siPwiplqIcGN5tW1XPU16-XFg1EoK7jc5IU3BKoEHYnwZo7Rmuh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Prc4b6bcJm8M1Q9xZhhsElisuUyVTN9-dDMNUZ1h0tUe9oGsZYLh9vjEsMokqBXFM_1igHOfgRn4I17Xt8EBMZI9cEjakByjv-g5Pt9tG69RQm765HLhf8VpafvE5Z3BwDpZs4x5uMkVDURT9qcA&amp;hl=en Server: quimby_frontend Content-Length: 1566 Content-Type: text/html; charset=UTF-8 Expires: Wed, 26 Mar 2014 00:30:07 GMT Alternate-Protocol: 80:quic X-Content-Type-Options: nosniff X-Frame-Options: SAMEORIGIN X-XSS-Protection: 1; mode=block
  302 Moved
  
  The document has moved here. HTTP/1.0 302 Found Cache-Control: public, max-age=21600 Date: Tue, 25 Mar 2014 18:30:07 GMT Age: 16 Location: http://www.google.com/search?tbs=sbi:AMhZZisAo2ZcfY19aFUJcEj26M4zKc9ZuxzfsUPzLuUJk-pd-siPwiplqIcGN5tW1XPU16-XFg1EoK7jc5IU3BKoEHYnwZo7RmuhyF5p9qaZwSgq4FKRkNW44JgzTi4Mr8g6ezNMQ6YzaAEQ-uFbPMNzY40NrE3uB7ePm4BGNowF34PiIjLOiVLkWwQ7sRoBVMoVgzBbAP7rDwHee5LyGF8Dq6QOT1TEhsURduPD6exzITyRl77agELdpTFSi-JXDncI6c4KdcuQYSx2LknnIW6nippmpPf3X5OYGn1CFZw13rlFPitLSY0Ang0COuSXKdpBy6B8Dak9QZNZ9VFB4HBRfnMFiyuBvQtyhAg2LeOnRbjnunGB0P1RlwKBF4hRId7wUdTu4Dfab5DQu9hGauLKcd7GcP4g-jQXx_1gymwDdZnPXLzZp1mkjVMX9GFSppj-IRWp3FVVqChsPEzKXdraevuWJFukjUdF87dU_1kLKO23lC8L3kusy05zcq7ZxyF1dHNfQ0vYJeWumtbRosJNuEcqiSyVW_1-bF104HMJLdCA0gr5VyIZolkcZok4W1sgjFYTWvfj6f0proaGE24HSO4Ov2hmhAy9HQUCr3e-KjgqyP4AOtlmI3VsuLu34jKSo0t4tWbb5PVBi1_1oebuv4oisdVdw22a6CRH2tiw8wg6Ya1VgxsXhyj8U7lrQ8cBHVDKlOI6EimXtnELBHyDNQT1Zpsz1hK10GYvFaRNMFd7Rqmg87CLdycgyRV-sYxNWxIu9agNgHTwuU1W-GgeWWcM9noeMwgqMKSGh9lt_1hda3ZWrcA4Y1MeiG55b4ZYvOjcm9t9iIy6LA2S4AjC2X1qZHvJtSqzgfOz8yTuX5jUHqCl0jI1FdOSmqZV1GqQ0uaJfsuchlsWUULfUJBzFiGkAuOqIzU0bpXLNqLHoYPJUPwr66H6jWPFLsWAS9_1GRNj70s30jfbzcS0NUShUvE2meUhlpx-f5M0nmS0zvf-3OQOUkXlYO2VUZ4x9y8G76hHoTkDxqzhhGrgohyFmkUvAWmSkHTBpbP6gek8cyrmBnXuedSV3r2O71G8CUbdHFxfIO8FWlkGj1cUYu60PoKF6hndjZsOlV-dSNXfOTKeC1jPtf5ycXA0s0xLK7_1K0iWxhfmVq62WgQ4O3Prc4b6bcJm8M1Q9xZhhsElisuUyVTN9-dDMNUZ1h0tUe9oGsZYLh9vjEsMokqBXFM_1igHOfgRn4I17Xt8EBMZI9cEjakByjv-g5Pt9tG69RQm765HLhf8VpafvE5Z3BwDpZs4x5uMkVDURT9qcA&amp;hl=en Server: quimby_frontend Content-Length: 1566 Content-Type: text/html; charset=UTF-8 Expires: Wed, 26 Mar 2014 00:30:07 GMT Alternate-Protocol: 80:quic X-Content-Type-Options: nosniff X-Frame-Options: SAMEORIGIN X-XSS-Protection: 1; mode=block
  302 Moved
  
  The document has moved here.
  </code></pre>