Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the first URL of an image search result with google image API in PHP

Tags:

php

image

did you know a php script (a class will be nice) who get the url of the first image result of a google api image search? Thanks

Example.

<?php echo(geturl("searchterm")) ?>
like image 447
DomingoSL Avatar asked Jun 10 '10 20:06

DomingoSL


People also ask

Does Google Image Search have an API?

Google Reverse Image API allows to get results for image search https://images.google.com . The API is accessed through the following endpoint: /search? engine=google_reverse_image .


2 Answers

I have found a solution to get the first image from Google Image result using Simple HTML DOM as Sarfraz told.

Kindly check the below code. Currently it is working fine for me.

$search_keyword=str_replace(' ','+',$search_keyword);
$newhtml =file_get_html("https://www.google.com/search?q=".$search_keyword."&tbm=isch");
$result_image_source = $newhtml->find('img', 0)->src;
echo '<img src="'.$result_image_source.'">';
like image 128
Gunaseelan Avatar answered Sep 28 '22 08:09

Gunaseelan


You should be able do that easily with Simple HTML DOM.

Note: See the examples on their site for more information.

  • A HTML DOM parser written in PHP5+ let you manipulate HTML in a very easy way!
  • Find tags on an HTML page with selectors just like jQuery.
like image 36
Sarfraz Avatar answered Sep 28 '22 08:09

Sarfraz