0
누군가가 임의의 이미지 URL을 얻는 방법을 알고 있는지 궁금해 Google에서 PHP로? 이 코드가 있지만 작동하지 않는 경우 ("검색 중 오류"가 두 번 표시되는 오류)임의의 이미지 URL Google에서
<?
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='&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&start=64&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&start=52&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
스택 오버플로에 오신 것을 환영합니다. "작동하지 않는다"는 결코 좋은 에러 설명이 아니다. 무엇이 잘못되었는지, 어떤 오류 메시지가 나옵니까? 등등을 설명하십시오. –
HTML을 실제로 구문 분석하여 구형이 될 수있는 매개 변수로 분할하는 것이 좋습니다. –
오류 로그 읽기 :'file_get_contents'는 다른 호스트에 연결할 수 없습니다 (URL 앞의'http : //'는 금지되어 있습니다). 이 코드는 저에게 효과적이므로 PHP 구성을 편집하면 문제가 해결됩니다. – Chris