Return Google Image Search Results in HTML using PHP
20,319
Solution 1
function get_url_contents($url) {
$crl = curl_init();
curl_setopt($crl, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)');
curl_setopt($crl, CURLOPT_URL, $url);
curl_setopt($crl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($crl, CURLOPT_CONNECTTIMEOUT, 5);
$ret = curl_exec($crl);
curl_close($crl);
return $ret;
}
$json = get_url_contents('http://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=sausages');
$data = json_decode($json);
foreach ($data->responseData->results as $result) {
$results[] = array('url' => $result->url, 'alt' => $result->title);
}
print_r($results);
Output:
Array
(
[0] => Array
(
[url] => http://upload.wikimedia.org/wikipedia/commons/thumb/c/c4/Salchicha_oaxaque%25C3%25B1a.png/220px-Salchicha_oaxaque%25C3%25B1a.png
[alt] => Sausage - Wikipedia, the free encyclopedia
)
[1] => Array
(
[url] => http://upload.wikimedia.org/wikipedia/commons/c/c1/Reunion_sausages_dsc07796.jpg
[alt] => File:Reunion sausages dsc07796.jpg - Wikimedia Commons
)
[2] => Array
(
[url] => http://1.bp.blogspot.com/-zDyoLPoM1Zg/ULXDPba_2iI/AAAAAAAAAAs/QzfNNmDFmzc/s1600/shop_sausages.jpg
[alt] => Maik's Yummy German Sausage
)
[3] => Array
(
[url] => http://sparseuropeansausage.com/images/sausage-web/sausagesBiggrilling2.jpg
[alt] => Spar's European Sausage Shop
)
)
Showing the images:
<?php foreach($results as $image): ?>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>"/><br/>
<?php endforeach; ?>
Edit after comments:
$url = 'http://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=' . get_the_title();
$json = get_url_contents($url);
Solution 2
It should be noted that this API is no longer available from Google, and will always return an error code.
Author by
Alan Carr
Updated on June 01, 2020Comments
-
Alan Carr about 4 years
I know how we can use the Google API to return image results in AJAX, but I want to be able to return images for a specific query and then output them in to HTML on my page.
For example:
http://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=sausages
Returns results with infomation and images about the top 10 results for the keyword sausages.
How can I query this url to output the images and titles of the images on my page using PHP in HTML.
I am using the following at the top of the function to return the title:
$tit = get_the_title();
Then I am apending it here:
$json = get_url_contents('http://ajax.googleapis.com/ajax/services/search/images?v=1.0&q='.$tit.'');
But it won't recognize the title
-
Alan Carr about 11 yearsHi Enenen, Thanks, ok that works, but how could I output it plain html without all the array details. So just the image url and the title, in plain text?
-
Alan Carr about 11 yearsOk, well at the monet it prionts all the array information like: [0] => Array, I just want to be able to print the url and the title.
-
enenen about 11 yearsThen in the loop (
foreach ($data->responseData->results as $result)
) you can just useecho '<img src="' . $result->url . '" alt="' . $result->alt . '"/><br/>';
without storing the information in an array. -
Alan Carr about 11 yearsLast question... I'm using this in wordpress and have added: $tit = get_the_title(); to the top of the code. I'm then appending that to the end of the query using .$tit. but it's not working, any ideas?
-
Alan Carr about 11 yearsThis is what I'm using and it's not recognizing it
-
Alan Carr about 11 yearsNow I get this error: Warning: Invalid argument supplied for foreach() in /home/movies/public_html/wp-content/themes/moviestv/single.php on line 123
-
Reham Fahmy over 9 years@AlanCarr did you fix it! cause i'm having the same problem and don't know what wrong .. wired so please if you fixed it, share it.
-
Android Rao almost 6 yearsIt returns value "thumb"