根据搜索结果获取Flickr图片

get Flickr images depending on a search result

本文关键字:Flickr 图片 获取 搜索结果      更新时间:2023-09-26

如何根据文件名显示来自Flickr的某些图像。我想搜索图像,只显示那些适合我的搜索结果。

老实说,这是我第一次使用Flickr,他们真的需要在那里放更多的例子。

知道从哪里开始吗?

这是我拼凑的一个帮助器方法,它允许您向flickr的api发出请求。浏览一下flickr api文档可能会有所帮助,这样您就可以开始弄清楚如何处理将返回的数据。这应该可以在Chrome和Firefox中工作,我还没有在IE或Safari中测试过。

/*
 * Make an XmlHttpRequest to api.flickr.com/services/rest/ with query parameters specified
 * in the options hash. Calls cb once the request completes with the results passed in.
 */
var makeFlickrRequest = function(options, cb) {
  var url, xhr, item, first;
  url = "http://api.flickr.com/services/rest/";
  first = true;
  for (item in options) {
    if (options.hasOwnProperty(item)) {
      url += (first ? "?" : "&") + item + "=" + options[item];
      first = false;
    }
  }
  xhr = new XMLHttpRequest();
  xhr.onload = function() { cb(this.response); };
  xhr.open('get', url, true);
  xhr.send();
};

用法:

var options = { 
  "api_key": "<your api key here>",
  "method": "flickr.photos.search", // You can replace this with whatever method,
                                  // flickr.photos.search fits your use case best, though.
  "format": "json",
  "nojsoncallback": "1",
  "text": "<your search text here>"  // This is where you'll put your "file name"
}
makeFlickrRequest(options, function(data) { alert(data) }); // Leaving the actual 
                                                            // implementation up to you! ;)

如果你正在使用jQuery,这里有一个jQuery版本:

var makeFlickrRequest = function(options, cb) {
  var url, item, first;
  url = "http://api.flickr.com/services/rest/";
  first = true;
  $.each(options, function(key, value) { 
    url += (first ? "?" : "&") + key + "=" + value;
    first = false; 
  });
  $.get(url, function(data) { cb(data); });
};

此方法与非jquery版本的用法相同