2012-02-25 3 views
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='&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 
+2

스택 오버플로에 오신 것을 환영합니다. "작동하지 않는다"는 결코 좋은 에러 설명이 아니다. 무엇이 잘못되었는지, 어떤 오류 메시지가 나옵니까? 등등을 설명하십시오. –

+0

HTML을 실제로 구문 분석하여 구형이 될 수있는 매개 변수로 분할하는 것이 좋습니다. –

+2

오류 로그 읽기 :'file_get_contents'는 다른 호스트에 연결할 수 없습니다 (URL 앞의'http : //'는 금지되어 있습니다). 이 코드는 저에게 효과적이므로 PHP 구성을 편집하면 문제가 해결됩니다. – Chris

답변

1

내가 지금하려고하는 일을 수행했고, 내 학습의 매우 초기 단계였다. 어쨌든 당신이 당신을 도울 것입니다 희망 ... 다음 XPath는 또는 정규식, 당신은 그 일을 할 수 있으며,이 first

사용하여 컬을 참조하십시오 ..

이러한 질문에 유래에 여기에 제공된 답변을 공부할 수 있습니다 많이

관련 문제