2010-06-10 12 views
1

트위터의 트렌드 주제와 관련하여 백그라운드 이미지가 변경되는 프로그램을 개발 중입니다. 그래서 저는 저에게 한 단어 (트위터에 첫 번째 TT)를 반환하는 함수를 가지고 있습니다. 그런 다음 Google 이미지 검색이나 플리커와 같이 해당 단어에 상대적인 이미지 URL을 제공하는 또 다른 함수가 필요합니다.이미지 URL을 PHP로 가져 오기

이 작업을 수행하는 방법을 알고 계십니까? 또는 더 나은, 당신은 만든 PHP 스크립트가 있습니까? 요약

: 구글 이미지 또는 플리커 검색을 수행하고,

+1

결과에 놀랄 준비를하십시오. – zneak

+1

오해의 소지가있는 사람들을 위해 : 그는 구글 이미지 나 플리커 검색을 할 수있는 기능을 원하고 그가 가지고있는 단어의 이미지에 대응하는 URL을 준다. –

+1

@zneak : 재미있다! 확실하게 Google 이미지 검색은 '안전한'것으로 판단되는 이미지를 필터링하는 매개 변수를 제공합니다. 분명히 보장 할 수는 없지만 뭔가 있습니다. :-) – artlung

답변

2

Google's AJAX Search API 이미지를 검색 할 수 있습니다. Video and Image Search Examples, PHP를 사용하여 확실하지 않은 결과를 구문 분석 할 수 있는지 여부. 반환 된 데이터를 구문 분석하여 배경으로 적용 할 이미지 URL을 추출 할 수 있습니다. 시도 할 Yahoo Image Search APIflickr Search API도 있습니다.

Google AJAX Documentation에는 PHP를 사용하여 검색을 호출하는 방법을 보여주는 PHP code snippet이 있습니다.

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's, Animation, Comics, And Interesting Movies. « Lizilla 
          [titleNoFormatting] => Superhero'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 
) 

이의 까다로운 측면은 당신이 할 수 있다는 것이다 :

여기
$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 디코딩 할 때 보이는 반환 내용은 다음과 같습니다 여기

은 "배트맨"에 대한 이미지 검색 결과를 찾아 코드의 이 이미지를 사용할 권한이 필요합니다. 일부 테스트에서는 공개 도메인을 제한하면 결과에서 훨씬 적은 수의 이미지를 얻는 것처럼 보입니다. 또한 실제로 사용할 수있는 서버로 제한 할 수있는 메커니즘이 없기 때문에 유효한 URL을 얻을 수 있지만 이미지를 사용할 수없는 경우도 있습니다. 간단한 요청처럼 보이지만 URL 처리에 다른 문제가 있습니다.

+0

이 코드는 나를 위해 일했습니다 ... 4 개 이상의 리츠 츠를 얻을 수 있는지 물어볼 수 있습니까? – faq

+2

@faq - API가 사용되지 않는 것처럼 보입니다. 하지만 당신은 페이지 매김을 정의하는 커서 객체를 봐야한다고 생각합니다. – artlung

1

답변을 게시 저를 용서해주세요 단어에 대한 이미지에 해당 URL을 반환하는 기능이있다,하지만 난하지 않습니다 그러나 50 평판을 가지고 있으므로 의견을 남길 수는 없습니다.

이미지를 가져 오는 위치를 말하지 않습니다.

로컬에 저장되는 경우 엄청난 수의 이미지가 필요하고 여전히 일치하는 항목을 찾을 수 없다는 점을 제외하고는 간단해야합니다.

그렇지 않은 경우 일 수 있습니다. Google 이미지에서 첫 번째 이미지를 가져옵니다. 그러나 저작권 문제에 대해 알고 있어야합니다. Creative Commons 라이센스하에 이미지 라이센스로 가득 찬 사이트를 찾고 싶을 수 있습니다.

@zneak이 말했듯이, 한 단어를 사용하면 종종 놀라운 결과를 얻을 수 있습니다.