How to get images from Google images with PHP


/ Published in: PHP

This simply php function will retrieve the links of the images from Google Images searched with a keyword. The function just calls images.google.it and parse the html to find the url of the images, in this case the urls are stored in the javascript, so the parsing doesn’t use any html tag to find data. It builds an array of urls and return it.


Copy this code and paste it in your HTML
  1. function getGoogleImg($k) {
  2. $url = "http://images.google.it/images?as_q=##query##&hl=it&imgtbs=z&btnG=Cerca+con+Google&as_epq=&as_oq=&as_eq=&imgtype=&imgsz=m&imgw=&imgh=&imgar=&as_filetype=&imgc=&as_sitesearch=&as_rights=&safe=images&as_st=y";
  3. $web_page = file_get_contents( str_replace("##query##",urlencode($k), $url ));
  4.  
  5. $tieni = stristr($web_page,"dyn.setResults(");
  6. $tieni = str_replace( "dyn.setResults(","", str_replace(stristr($tieni,");"),"",$tieni) );
  7. $tieni = str_replace("[]","",$tieni);
  8. $m = preg_split("/[\[\]]/",$tieni);
  9. $x = array();
  10. for($i=0;$i<count($m);$i++) {
  11. $m[$i] = str_replace("/imgres?imgurl\\x3d","",$m[$i]);
  12. $m[$i] = str_replace(stristr($m[$i],"\\x26imgrefurl"),"",$m[$i]);
  13. $m[$i] = preg_replace("/^\"/i","",$m[$i]);
  14. $m[$i] = preg_replace("/^,/i","",$m[$i]);
  15. if ($m[$i]!="") array_push($x,$m[$i]);
  16. }
  17. return $x;
  18. }

URL: http://www.barattalo.it/2010/03/30/php-google-images-mini-bot/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.