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
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);
Sorry, I should be posting this as a comment to enenen's answer. Unfortunately I don't have the reputation to be able to do so at the moment.
Enenen's answer worked really well, I however came across some images that refused to load correctly for me. It might be obvious to others, but it took me a while to figure it out.
Some of the returned URLs have encoded characters that weren't picked up properly by the browser and resulted in a broken image. This was only every 1 in 50 images or so.
The string returned was for example: http://pix4.agoda.net/hotelimages/106/10615/10615_14033018230018907115.jpg%3Fs%3D800x600
Solved this by using the PHP function urldecode on $image['url']
$img_url=urldecode($image['url']);
echo '<img src="'.$img_url.'" alt="'.$image['alt'].'">';
New string that is working properly in my browser (%3F and %3D decoded) http://pix4.agoda.net/hotelimages/106/10615/10615_14033018230018907115.jpg?s=800x600
It should be noted that this API is no longer available from Google, and will always return an error code.
Forget About Google and get the same images URL's using Bing.com
Example searches work fine on my website.
You can give this function a try:
The function returns an array of images found. You can opt out more for your custom parameters.
function searchImage($search){
$url="http://www.bing.com/images/search?pq=".urlencode(strtolower($search))."&count=50&q=".urlencode($search);
$data=file_get_contents($url);
$rr=explode("<div class=\"item\">", $data);
$execc="";
for($r=2;$r<(count($rr));$r++){
$nf=explode("\"", $rr[$r]);
$nextFile=$nf[1];
$no="stock;123;dreams";
$x=true;
$tt=explode(";", $no);
for($a=0;$a<count($tt);$a++){
if(strpos($nextFile, $tt[$a])!=false){
$x=false;
}
}
if($x==true){
$nextFil[]=$nextFile;
}
}
return $nextFil;
}