Get Google Search Images using php

Tony Stark picture Tony Stark · May 9, 2013 · Viewed 23.4k times · Source

Search on Google images with car keyword & get car images.

enter image description here

I found two links to implement like this,

  1. PHP class to retrieve multiple images from Google using curl multi handler
  2. Google image API using cURL

implement also but it gave 4 random images not more than that. enter image description hereenter image description hereenter image description hereenter image description here

Question: How to get car images in PHP using keyword i want to implement like we search on Google?

Any suggestion will be appreciated!!!

Answer

user3189338 picture user3189338 · Jan 5, 2015

You could use the PHP Simple HTML DOM library for this:

<?php
    include "simple_html_dom.php";
    $search_query = "ENTER YOUR SEARCH QUERY HERE";
    $search_query = urlencode( $search_query );
    $html = file_get_html( "https://www.google.com/search?q=$search_query&tbm=isch" );
    $image_container = $html->find('div#rcnt', 0);
    $images = $image_container->find('img');
    $image_count = 10; //Enter the amount of images to be shown
    $i = 0;
    foreach($images as $image){
        if($i == $image_count) break;
        $i++;
        // DO with the image whatever you want here (the image element is '$image'):
        echo $image;
    }

This will print a specific number of images (number is set in '$image_count').

For more information on the PHP Simple HTML DOM library click here.