Random Image URL From Google

i am wondering if someone knows how to get a random Image URL From Google in php ?
if got this code but its not working.

[php]<?php
function GetRandomImageURL($topic=’’, $min=0, $max=100)
{
// get random image from Google
if ($topic==’’) $topic=‘image’;
$ofs=mt_rand($min, $max);
$geturl=‘http://www.google.com/images?q=’ . $topic . ‘&start=’ . $ofs . ‘&gbv=1’;
$data=file_get_contents($geturl);

$f1=’

’;
$f2=’<a href="/imgres?imgurl=’;
$f3=’&imgrefurl=’;

$pos1=strpos($data, $f1)+strlen($f1);
if ($pos1==FALSE) return FALSE;
$pos2=strpos($data, $f2, $pos1)+strlen($f2);
if ($pos2==FALSE) return FALSE;
$pos3=strpos($data, $f3, $pos2);
if ($pos3==FALSE) return FALSE;
return substr($data, $pos2, $pos3-$pos2);
}

?>[/php]

Thank you for your help and time in advance.

Hello kzip, here i am going to provide you a full script for load random image from Google based on topic pass by you. Save below code as loadrandom_googleimg.php [php] # Save below code as loadrandom_googleimg.php <? function GetRandomImageURL($topic='', $min=0, $max=100) { // get random image from Google if ($topic=='') $topic='image'; $ofs=mt_rand($min, $max); $geturl='http://www.google.com/images?q=' . $topic . '&start=' . $ofs . '&gbv=1'; $data=file_get_contents($geturl);
$f1='<div id="center_col">';
$f2='<a href="/imgres?imgurl=';
$f3='&amp;imgrefurl=';

$pos1=strpos($data, $f1)+strlen($f1);
if ($pos1==FALSE) return FALSE;
$pos2=strpos($data, $f2, $pos1)+strlen($f2);
if ($pos2==FALSE) return FALSE;
$pos3=strpos($data, $f3, $pos2);
if ($pos3==FALSE) return FALSE;
return substr($data, $pos2, $pos3-$pos2);

}

function ShowRandomImage($topic=’’)
{
echo(’

’);
$url=GetRandomImageURL($topic);
if ($url==FALSE) echo(‘Error while searching’);
else {
echo(htmlentities($url) . ‘
’);
echo(’’);
}
echo(’
’);
}

ShowRandomImage();
#you can chage flower with any topic which you want to load image.
ShowRandomImage(‘flower’);
?>
[/php]
I hope this will helpful for you.
Reply your feed back
SR

Hi Sarthak Patel

thanks for the help im getting an error it shows “Error while searching” twice

i know what the problem is i need to use curl on my websurfer anyone have a idea how to convert this code to curl ??

Try placing an echo just before your " $data=file_get_contents($geturl);" to show you what the url actually is.
Something like:
echo $geturl;
die();

This will show you what you are trying to capture from Google’s site.
I am guessing you are not sending your URL correctly. (Missing quote or something.)
This will show you if it is not formatted correctly… Good luck.

Sponsor our Newsletter | Privacy Policy | Terms of Service