0

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. (error it shows "Error while searching" twice)

<?
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('<table border="1"><tr><td>');
    $url=GetRandomImageURL($topic);
    if ($url==FALSE) echo('Error while searching');
    else {
      echo(htmlentities($url) . '<br />');
      echo('<img width="500" src="' . $url . '" />');
    }
    echo('</td></tr></table>');
  }

  ShowRandomImage();
#you can chage flower with any topic which you want to load image.
  ShowRandomImage('flower');
?>

Thank you for your help and time in advance.

Error log:

[25-Feb-2012 16:38:00] PHP Warning:  file_get_contents() [<a href='function.file-get-contents'>function.file-get-contents</a>]: http:// wrapper is disabled in the server configuration by allow_url_fopen=0 in /home/public_html/images/test.php on line 8
[25-Feb-2012 16:38:00] PHP Warning:  file_get_contents(http://www.google.com/images?q=image&amp;start=64&amp;gbv=1) [<a href='function.file-get-contents'>function.file-get-contents</a>]: failed to open stream: no suitable wrapper could be found in /home/public_html/images/test.php on line 8
[25-Feb-2012 16:38:00] PHP Warning:  strpos() [<a href='function.strpos'>function.strpos</a>]: Offset not contained in string in /home/public_html/images/test.php on line 16
[25-Feb-2012 16:38:00] PHP Warning:  strpos() [<a href='function.strpos'>function.strpos</a>]: Offset not contained in string in /home/public_html/images/test.php on line 18
[25-Feb-2012 16:38:00] PHP Warning:  file_get_contents() [<a href='function.file-get-contents'>function.file-get-contents</a>]: http:// wrapper is disabled in the server configuration by allow_url_fopen=0 in /home/public_html/images/test.php on line 8
[25-Feb-2012 16:38:00] PHP Warning:  file_get_contents(http://www.google.com/images?q=flower&amp;start=52&amp;gbv=1) [<a href='function.file-get-contents'>function.file-get-contents</a>]: failed to open stream: no suitable wrapper could be found in /home/public_html/images/test.php on line 8
[25-Feb-2012 16:38:00] PHP Warning:  strpos() [<a href='function.strpos'>function.strpos</a>]: Offset not contained in string in /home/public_html/images/test.php on line 16
[25-Feb-2012 16:38:00] PHP Warning:  strpos() [<a href='function.strpos'>function.strpos</a>]: Offset not contained in string in /home/public_html/images/test.php on line 18
john de groof
  • 41
  • 1
  • 4
  • 2
    Welcome to Stack Overflow. "It doesn't work" is *never* a good error description. Please describe what goes wrong, what error messages you get, etc. – Pekka Feb 25 '12 at 21:36
  • Can I recomend you actually parse the HTML rather than splitting it up with parameters that may quickly become outdated. – j_mcnally Feb 25 '12 at 21:41
  • 2
    Read the error log: `file_get_contents` is not allowed to connect to other hosts (as `http://` in front of URLs is forbidden). The code is working for me, so editing your PHP configuration will solve the problem. – Chris Feb 25 '12 at 21:41
  • PHP safe mode, use something like libcurl or Snoopy. – j_mcnally Feb 25 '12 at 21:42

1 Answers1

1

i have done something you are trying now, and it was a very initial stages of my learning. anyhow you can study these question and answers provided here on stackoverflow..

See this first

using curl, then xpath or regex, you can do the job..

hoping this will help you a lot

Community
  • 1
  • 1
Zaffar Saffee
  • 6,167
  • 5
  • 39
  • 77