来自谷歌的随机图像URL


Random Image URL From Google

我想知道是否有人知道如何在php中从谷歌获得随机图像URL?如果得到了这个代码,但它不起作用。(错误显示"搜索时出错"两次)

<?
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');
?>

感谢您的帮助和提前安排的时间。

错误日志:

[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

我已经做了一些你现在正在尝试的事情,这是我学习的最初阶段。无论如何,你可以在stackoverflow上研究这些问题和答案。。

查看第一个

使用curl,然后使用xpath或regex,就可以完成这项工作。。

希望这能对你有很大帮助