Get first 2-3 images (file_get_contects)

Discussion in 'PHP' started by freebsd, Aug 1, 2010.

  1. #1
    How i can get last 3 images from google/or/yahoo images with search word is ''movies"

    Example:
    http://www.google.bg/images?hl=bg&source=imghp&biw=1366&bih=575&q=movies&gbv=2&aq=f&aqi=&aql=&oq=&gs_rfai=
    Thanks...
     
    freebsd, Aug 1, 2010 IP
  2. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #2
    I have no idea about google because they recently changed there search page results, heres something to get you started using yahoo, its probable not the best method but its a start.

    
    <?php
    $url = "http://uk.images.search.yahoo.com/search/images?vc=&p=movies&toggle=1&cop=mss&ei=UTF-8&fr=yfp-t-710";
    
    function get_data($url)
    {
    $ch = curl_init();
     	$useragent="Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1";
    	curl_setopt($ch, CURLOPT_HEADER, 0);
    	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Set curl to return the data instead of printing it to the browser.
    	curl_setopt($ch, CURLOPT_URL, $url);
    	curl_setopt ($ch, CURLOPT_REFERER, 'http://www.mse360.com/about/bot.php');
     	curl_setopt ($ch, CURLOPT_USERAGENT, "MSE360 - FredBot (See: http://mse360.com/about/bot.php)");
    	curl_setopt ($ch, CURLOPT_TIMEOUT, 60);
    	$data = curl_exec($ch);
    	curl_close($ch);
    	return $data;
    }
    
    function get_match($regex,$content)
    {
    	preg_match_all($regex,$content,$matches);
        return $matches[0][0] . ' , ' . $matches[0][1] . ' , ' . $matches[0][2];
    }
    
    $images = get_data($url);
    
    $images = get_match('#src="http://thm-a0[0-9].yimg.com/nimage/(.+)"#isU',$images);
    
    $images = preg_replace('#src="#','',$images);
    $images = preg_replace('#"#','',$images);
    
    //echo $images;
    
    $pic = explode(",", $images);
    
    // These are the image urls
    echo "$pic[0]","<br>"; // pic1
    echo "$pic[1]","<br>"; // pic2
    echo "$pic[2]","<br>"; // pic3
    
    // Or these are the images
    echo "<img src=\"$pic[0]\">","<br>"; // pic1
    echo "<img src=\"$pic[1]\">","<br>"; // pic2
    echo "<img src=\"$pic[2]\">","<br>"; // pic3
    
    ?>
    
    PHP:
     
    Last edited: Aug 1, 2010
    MyVodaFone, Aug 1, 2010 IP