2012-02-25 154 views
0

我想知道如果有人知道如何從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

歡迎來到堆棧溢出。 「它不工作」是*從來不是一個好的錯誤描述。請描述出了什麼問題,出現什麼錯誤消息等。 – 2012-02-25 21:36:13

+0

我可以推薦你實際解析HTML,而不是用可能很快變得過時的參數來分解它。 – 2012-02-25 21:41:33

+2

閱讀錯誤日誌:'file_get_contents'不允許連接到其他主機(因爲URL前面的「http://」是被禁止的)。代碼正在爲我工​​作,所以編輯你的PHP配置將解決這個問題。 – Chris 2012-02-25 21:41:56

回答

1

我做了什麼,現在你想,這是我學習的最初階段。 無論如何,你可以研究這些問題,並在這裏提供了計算器的答案..

請參閱本使用first

捲曲,然後XPath或正則表達式,你可以做的工作..

希望這會幫助你很多