我正在开发一个程序,其背景图像将根据twitter的热门话题进行更改。所以我有一个函数可以给我一个单词(推特上的第一个TT),然后我需要另一个函数来给我一个相对于该单词的图像的网址,比如Google图像搜索或flickr。
你知道怎么做吗?甚至更好,你有一个PHP脚本吗?
总结:是否有一个功能可以进行谷歌图片或flickr搜索,并将相应的网址返回到图片
答案 0 :(得分:2)
Google's AJAX Search API允许您搜索图片:Video and Image Search Examples,但是您是否可以使用PHP来解析我不确定的结果。您当然可以解析返回的数据以提取图像URL以应用为背景。还可以尝试Yahoo Image Search API和flickr 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'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
)
这方面的一个棘手问题是您可能需要获得使用这些图像的权限。在某些测试中,看起来当您通过公共域限制时,结果中的图像会少得多。此外,没有机制限制到当时实际可用的服务器,因此有时您可能会获得有效的URL但没有可用的图像。这似乎是一个简单的请求,但它在url处理中引入了其他问题。
答案 1 :(得分:1)
请原谅我发布答案,但我还没有50点声望,所以我不能发表评论。
您没有说明您获取图像的位置。
如果它们存储在本地,那么它应该是直截了当的,除了你需要大量的图像并且必须接受你仍然可能找不到匹配。
如果没有,你可以从谷歌图片中抓取第一张图片。但是,请注意可能的版权问题;您可能希望在Creative Commons许可下找到一个充满图像许可的网站。
无论如何,正如@zneak所说,如果你使用一个单词,你通常会得到令人惊讶的结果。