从flickr api响应中创建img链接

时间:2013-08-12 11:37:23

标签: jquery flickr

我需要通过标题和响应创建链接从flickr获取第一张图片。但我找不到照片元素,我不知道为什么。我的代码:

var options = { 
  "api_key": API-KEY,
  "method": "flickr.photos.search",
  "accuracy": "11",
  "content_type": "1",
  "format": "xmlrpc",
  "per_page": "1",
  "text": TITLE
}

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) {
        $(data).find('photo').each(function(){
            cb($(this));
        });
    });
};

当我将代码更改为

时,我唯一能得到的就是来自string元素的文本
$(data).find('string').each(function(){
    cb($(this).text());
});

但我想要使用元素属性,比如解析文本:

<photos page="1" pages="222177" perpage="1" total="222177">
    <photo id="9494235388" owner="16159287@N02" secret="f5dbd3b43a" server="7443"    farm="8" title="2013 River Cruise Day 13 Bratislava" ispublic="1" isfriend="0" isfamily="0" />
</photos>

Api回应:

<methodResponse><params><param><value><string>
    <photos page="1" pages="222189" perpage="1" total="222189">
        <photo id="9494235388" owner="16159287@N02" secret="f5dbd3b43a" server="7443" farm="8" title="2013 River Cruise Day 13 Bratislava" ispublic="1" isfriend="0" isfamily="0" />
    </photos>
</string></value></param></params></methodResponse>

1 个答案:

答案 0 :(得分:1)

afaik您可以将format选项更改为"json"

var options = { 
  "api_key": API-KEY,
  "method": "flickr.photos.search",
  "accuracy": "11",
  "content_type": "1",
  "format": "json",                    //<--- JSON format
  "per_page": "1",
  "text": TITLE
}

这样,解析响应会更容易,我认为......

$.get(url, function(data) {
    $.each(data.photos, function(){
        console.log(this);  //<-- your photo object
    });
});