来自Google的随机图片网址

时间:2012-02-25 21:35:07

标签: 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='<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

1 个答案:

答案 0 :(得分:1)

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

请参阅此first

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

希望这会对你有很大的帮助