justincouch

Google Image By URL curl

Jan 22nd, 2013
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.98 KB | None | 0 0
  1. <?
  2.  
  3. $GOOGLE_URL = "http://www.google.com";
  4. $GOOGLE_SBI_URL = "/searchbyimage?image_url=";
  5. $IMAGE_URL = your_image_folder_url; // the folder where your images are
  6. $IMAGE_URL_ENCODED = your_image_folder_url_percent_encoded; // change the dashes and dots to % things
  7. $IMAGE_SUFFIX = $_GET['imgsfx']; // gets the image file name from the PHP call
  8.  
  9. // your browser info goes here. might be different. got mine from here: http://whatsmyuseragent.com/
  10. $AGENT_ID = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.101 Safari/537.11";
  11.  
  12. $url .= $GOOGLE_URL;
  13. $url .= $GOOGLE_SBI_URL;
  14. $url .= $IMAGE_URL_ENCODED;
  15. $url .= $IMAGE_SUFFIX;
  16.  
  17. $ch = curl_init();
  18. curl_setopt($ch, CURLOPT_URL, $url);
  19. curl_setopt($ch, CURLOPT_USERAGENT, $AGENT_ID);
  20. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  21. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  22.  
  23. $body = curl_exec($ch);
  24. curl_close($ch);
  25.  
  26. // link matchstring gets all the image links
  27. $linkmatchstring = "/imgres\?imgurl\=(.*?)(\&amp|\%3F)/";
  28. // thumb match string gets the base64 byte data that it uses for thumbnails
  29. $thumbmatchstring = "/'imgthumb1','data:image\/jpeg;base64,(.*?)'\)/";
  30.  
  31. // regex match on the curl text
  32. preg_match_all($linkmatchstring, $body, $linkmatches);
  33. preg_match_all($thumbmatchstring, $body, $thumbmatches);
  34.  
  35. // matches come in arrays
  36. $linkoutput = $linkmatches[1][0];
  37. $thumboutput = $thumbmatches[1][0];
  38.  
  39. $filename .= "gimgcurldump";
  40. $filename .= $IMAGE_SUFFIX;
  41. $filename .= ".html";
  42.  
  43. $f = fopen($filename,"w");
  44.  
  45. // makes a new html page
  46. $htmloutput .= "<html>\n<head>\n</head>\n<body>\n<img src=\"";
  47. $htmloutput .= $IMAGE_URL;
  48. $htmloutput .= $IMAGE_SUFFIX;
  49. $htmloutput .= "\"/>\n";
  50. $htmloutput .= "<img src=\"";
  51. $htmloutput .= $linkoutput;
  52. $htmloutput .= "\"/>\n</body>\n</html>";
  53. fwrite($f,$htmloutput);
  54. fclose($f);
  55.  
  56. // This is where I was trying to get stuff back.
  57. //HttpResponse::setData($file);
  58. //HttpResponse::send();
  59.  
  60. echo($thumboutput);
  61. //echo($file);
  62.  
  63. ?>
Add Comment
Please, Sign In to add comment