Php 来自谷歌的随机图像URL

Php 来自谷歌的随机图像URL,php,Php,我想知道是否有人知道如何在php中从Google获取随机图像URL? 如果你得到了这个代码,但它不工作。(错误显示“搜索时出错”两次) 我做了一些你现在正在尝试的事情,这是我学习的一个非常初级的阶段。 无论如何,您可以研究stackoverflow上提供的这些问题和答案 看到这个了吗 使用curl,然后使用xpath或regex,就可以完成这项工作 希望这将对您有很大帮助欢迎来到Stack Overflow。“它不工作”从来不是一个好的错误描述。请描述出现了什么问题,您收到了什么错误消息,等等。

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


我做了一些你现在正在尝试的事情,这是我学习的一个非常初级的阶段。
无论如何,您可以研究stackoverflow上提供的这些问题和答案

看到这个了吗

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


希望这将对您有很大帮助

欢迎来到Stack Overflow。“它不工作”从来不是一个好的错误描述。请描述出现了什么问题,您收到了什么错误消息,等等。我可以建议您实际解析HTML,而不是使用可能很快过时的参数将其拆分。请阅读错误日志:
file\u get\u contents
不允许连接到其他主机(因为禁止URL前面的
http://
)。代码适合我,因此编辑PHP配置将解决问题。在PHP安全模式下,使用libcurl或Snoopy之类的工具。
<?
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