dsu89430 2010-06-10 23:40
浏览 89
已采纳

在PHP中获取带有单词的图像的URL

I'm developing a program whose background image will change in relation to the trending topics of twitter. So I have a function which returns me a word (first TT on twitter), then I need another function that will give me a url of an image relative to that word, like Google image search or flickr.

Do you know how to do this? or even better, do you have a php script made?

In summary: is there a function that will do a google image or flickr search, and return a corresponding url to an image for the word

  • 写回答

2条回答 默认 最新

  • dongyong2063 2010-06-10 23:52
    关注

    Google's AJAX Search API allows you to search for images: Video and Image Search Examples, though whether you could use PHP to parse the results I'm not sure. You could certainly parse the returned data to extract image urls to apply as backgrounds though. There is also a Yahoo Image Search API and flickr Search API to try.

    The Google AJAX Documentation has a PHP code snippet that shows how to call searches using PHP.

    Here's code that finds image search results for "batman":

    $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 ">";
    }
    

    Here's what the raw returned json looks like when decoded:

    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
    )
    

    A tricky aspect of this is that you may need permission to use these images. In some testing it looks like when you restrict by public domain you get far fewer images in result. Also, there's no mechanism to restrict to servers that are actually available at that time, so sometimes you may get a valid url but no image will be available. It seems like a simple request, but it introduces other issues in url handling.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于哈夫曼树应用得到一些问题
  • ¥15 使用sql server语句实现下面两个实验(需要代码和运行结果截图)
  • ¥20 用web解决,要给我一个完整的网页,符合上述的要求
  • ¥20 求个sql server代码和结果的图 两道题
  • ¥15 银河麒麟操作系统无法使用U盘
  • ¥100 寻找:光电二极管电路设计服务
  • ¥15 YOLOv5改进后的结构图
  • ¥15 全志v3s怎么设置高速时钟,使用的荔枝派zero开发板,串口2需要921600的波特率
  • ¥15 关于#单片机#的问题:Lora通讯模块hc-14电路图求内部原理图
  • ¥50 esp32 wroom 32e 芯片解锁