2010-06-10 117 views
1

我正在開發一個程序,它的背景圖像將根據twitter的熱門話題而改變。所以我有一個函數返回給我一個單詞(twitter上的第一個TT),然後我需要另一個函數,它會給我一個與該單詞相關的圖像的URL,例如Google圖像搜索或flickr。在PHP中獲取圖像的URL url

你知道如何做到這一點嗎?或者甚至更好,你有一個PHP腳本?

總結:有會做一個谷歌的圖像或Flickr搜索,並返回相應的圖片網址的單詞

+1

準備好讓結果感到驚訝。 – zneak 2010-06-10 23:42:06

+1

對於那些似乎有誤解的人來說:他想要一個可以進行谷歌圖片或flickr搜索的功能,併爲他提供相應的url來顯示他所擁有的單詞的圖像 – 2010-06-10 23:55:28

+1

@zneak:funny!雖然可以肯定,谷歌圖片搜索提供了一個參數來篩選他們認爲是「安全」的圖片 - 顯然這不是保證,但這是一些東西。 :-) – artlung 2010-06-11 01:21:59

回答

2

Google's AJAX Search API允許您搜索圖片:Video and Image Search Examples,雖然您是否可以使用PHP解析結果,但我不確定。您當然可以解析返回的數據以提取圖像網址作爲背景應用。還有一個Yahoo Image Search APIflickr Search API來嘗試。

Google AJAX Documentation的PHP code snippet顯示如何使用PHP調用搜索。

這裏的代碼,發現圖片搜索結果的「蝙蝠俠」:

$word = "batman"; 
$manual_referer = 'http://example.com/'; 

// See reference for how to modify search 
// http://code.google.com/apis/ajaxsearch/documentation/reference.html 
$args = array(
    'v' => '1.0', 
    'q' => $word, 
    'as_filetype' => 'jpg', 
    'imgsz' => 'medium', // image size 
    'safe' => 'active', // image "safeness" 
    'as_filetype' => 'jpg', 
); 
$url = "http://ajax.googleapis.com/ajax/services/search/images?"; 
foreach ($args as $key => $val) { 
    $url .= $key . '=' . rawurlencode($val) . '&'; 
} 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_REFERER, $manual_referer); 
$body = curl_exec($ch); 
curl_close($ch); 

$json = json_decode($body, true); 
$results = $json['responseData']['results']; 
foreach ($results as $result) { 
    print "<img src="; 
    print $result['url']; // here's your url 
    print ">"; 
} 

這裏是原始返回的JSON看起來解碼時這樣的:

Array 
(
    [responseData] => Array 
     (
      [results] => Array 
       (
        [0] => Array 
         (
          [GsearchResultClass] => GimageSearch 
          [width] => 240 
          [height] => 338 
          [imageId] => k8FYqFKsdhvu3M: 
          [tbWidth] => 84 
          [tbHeight] => 119 
          [unescapedUrl] => http://lizilla.files.wordpress.com/2009/08/batman.jpg 
          [url] => http://lizilla.files.wordpress.com/2009/08/batman.jpg 
          [visibleUrl] => lizilla.wordpress.com 
          [title] => Superhero&#39;s, Animation, Comics, And Interesting Movies. « Lizilla 
          [titleNoFormatting] => Superhero&#39;s, Animation, Comics, And Interesting Movies. « Lizilla 
          [originalContextUrl] => http://lizilla.wordpress.com/2009/08/26/superheros-animation-comics-and-interesting-movies/ 
          [content] => Dunanununanuna <b>BATMAN</b>! 
          [contentNoFormatting] => Dunanununanuna BATMAN! 
          [tbUrl] => http://images.google.com/images?q=tbn:k8FYqFKsdhvu3M::lizilla.files.wordpress.com/2009/08/batman.jpg 
         ) 

        [1] => Array 
         (
          [GsearchResultClass] => GimageSearch 
          [width] => 307 
          [height] => 290 
          [imageId] => faxJ90Dbo1TW1M: 
          [tbWidth] => 117 
          [tbHeight] => 111 
          [unescapedUrl] => http://www.solarnavigator.net/films_movies_actors/film_images/batman_michael_keaton_jack_nocholson_joker_marvel_comics.jpg 
          [url] => http://www.solarnavigator.net/films_movies_actors/film_images/batman_michael_keaton_jack_nocholson_joker_marvel_comics.jpg 
          [visibleUrl] => www.solarnavigator.net 
          [title] => <b>BATMAN</b> THE MOVIE 
          [titleNoFormatting] => BATMAN THE MOVIE 
          [originalContextUrl] => http://www.solarnavigator.net/films_movies_actors/batman.htm 
          [content] => <b>Batman</b> and the Joker in the 
          [contentNoFormatting] => Batman and the Joker in the 
          [tbUrl] => http://images.google.com/images?q=tbn:faxJ90Dbo1TW1M::www.solarnavigator.net/films_movies_actors/film_images/batman_michael_keaton_jack_nocholson_joker_marvel_comics.jpg 
         ) 

        [2] => Array 
         (
          [GsearchResultClass] => GimageSearch 
          [width] => 300 
          [height] => 300 
          [imageId] => nDWzhPnraNi_gM: 
          [tbWidth] => 116 
          [tbHeight] => 116 
          [unescapedUrl] => http://i192.photobucket.com/albums/z167/Great_WhiteSnark/batman_bale-1.jpg 
          [url] => http://i192.photobucket.com/albums/z167/Great_WhiteSnark/batman_bale-1.jpg 
          [visibleUrl] => www.coolchaser.com 
          [title] => <b>batman</b> MySpace graphics and comments 
          [titleNoFormatting] => batman MySpace graphics and comments 
          [originalContextUrl] => http://www.coolchaser.com/graphics/tag/batman 
          [content] => All Graphics » <b>batman</b> 
          [contentNoFormatting] => All Graphics » batman 
          [tbUrl] => http://images.google.com/images?q=tbn:nDWzhPnraNi_gM::i192.photobucket.com/albums/z167/Great_WhiteSnark/batman_bale-1.jpg 
         ) 

        [3] => Array 
         (
          [GsearchResultClass] => GimageSearch 
          [width] => 250 
          [height] => 302 
          [imageId] => W9EAV1DUDesHuM: 
          [tbWidth] => 96 
          [tbHeight] => 116 
          [unescapedUrl] => http://upload.wikimedia.org/wikipedia/en/thumb/f/f6/New_Batman_Adventures_cast.jpg/250px-New_Batman_Adventures_cast.jpg 
          [url] => http://upload.wikimedia.org/wikipedia/en/thumb/f/f6/New_Batman_Adventures_cast.jpg/250px-New_Batman_Adventures_cast.jpg 
          [visibleUrl] => en.wikipedia.org 
          [title] => The New <b>Batman</b> Adventures - Wikipedia, the free encyclopedia 
          [titleNoFormatting] => The New Batman Adventures - Wikipedia, the free encyclopedia 
          [originalContextUrl] => http://en.wikipedia.org/wiki/The_New_Batman_Adventures 
          [content] => The New <b>Batman</b> Adventures 
          [contentNoFormatting] => The New Batman Adventures 
          [tbUrl] => http://images.google.com/images?q=tbn:W9EAV1DUDesHuM::upload.wikimedia.org/wikipedia/en/thumb/f/f6/New_Batman_Adventures_cast.jpg/250px-New_Batman_Adventures_cast.jpg 
         ) 

       ) 

      [cursor] => Array 
       (
        [pages] => Array 
         (
          [0] => Array 
           (
            [start] => 0 
            [label] => 1 
           ) 

          [1] => Array 
           (
            [start] => 4 
            [label] => 2 
           ) 

          [2] => Array 
           (
            [start] => 8 
            [label] => 3 
           ) 

          [3] => Array 
           (
            [start] => 12 
            [label] => 4 
           ) 

          [4] => Array 
           (
            [start] => 16 
            [label] => 5 
           ) 

          [5] => Array 
           (
            [start] => 20 
            [label] => 6 
           ) 

          [6] => Array 
           (
            [start] => 24 
            [label] => 7 
           ) 

          [7] => Array 
           (
            [start] => 28 
            [label] => 8 
           ) 

         ) 

        [estimatedResultCount] => 3050000 
        [currentPageIndex] => 0 
        [moreResultsUrl] => http://www.google.com/images?oe=utf8&ie=utf8&source=uds&start=0&safe=active&imgsz=medium&as_filetype=jpg&hl=en&q=batman 
       ) 

     ) 

    [responseDetails] => 
    [responseStatus] => 200 
) 

這方面的一個棘手的方面是,你可能需要使用這些圖像的權限。在某些測試中,當您通過公共領域進行限制時,您看到的結果中的圖像會少得多。此外,沒有任何機制可以限制當時實際可用的服務器,因此有時您可能會獲得有效的url,但不會有圖像可用。這似乎是一個簡單的請求,但它引入了URL處理中的其他問題。

+0

此代碼爲我工作...我可以問,如果有可能獲得超過4次rezults? – faq 2012-09-08 23:18:42

+2

@faq - 它看起來像API已被棄用。但我認爲你需要看看定義分頁等的遊標對象。 – artlung 2012-09-10 00:29:44

1

請原諒我要發佈一個答案的功能,但我不但有50名聲望,所以我不能發表評論。

你不說你在哪裏得到你的圖片。

如果他們存儲在本地,那麼它應該是直截了當的,除了你將需要大量的圖像,並且必須接受你仍然可能找不到匹配。

如果不是,你可能只是從谷歌圖像抓住第一個圖像。 Hoever,注意可能的版權問題;您可能需要在Creative Commons許可證下找到一個完整的映像許可證。

在任何情況下,正如@zneak所說,如果您使用單個單詞,您經常會得到令人驚訝的結果。