Tony Stark
Tony Stark

Reputation: 8094

Get Google Search Images using php

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!!!

Upvotes: 5

Views: 26373

Answers (2)

user3189338
user3189338

Reputation:

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.

Upvotes: 6

user4247736
user4247736

Reputation:

i am not very much sure about this ,but still google gives a nice documentation about this.

 $url = "https://ajax.googleapis.com/ajax/services/search/images?" .
   "v=1.0&q=barack%20obama&userip=INSERT-USER-IP";   
// sendRequest
// note how referer is set manually
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, /* Enter the URL of your site here */);
$body = curl_exec($ch);
curl_close($ch);

// now, process the JSON string
$json = json_decode($body);
// now have some fun with the results...

this is from the official Google's developer guide regarding image searching. for more reference you can have a reference of the same here.

 https://developers.google.com/image-search/v1/jsondevguide#json_snippets_php

in $url you must set the search keywords.

Upvotes: 0

Related Questions