Jquery JSON Flickr API 返回一组照片

发布于 2024-10-07 04:44:40 字数 1962 浏览 5 评论 0原文

我试图从 Flickr 获取指定的图像集,然后使用 JSON 和 REST API 显示该集中的图像。这是我正在使用的代码:

    $.getJSON("http://api.flickr.com/services/rest/?method=flickr.photosets.getPhotos&api_key=xxx&set=72157623858739780&format=json&jsoncallback=?", function(data){
  $.each(data.items, function(i,item){
    $("<img/>").attr("src", item.media.m).appendTo("#images");
  });
});

我从示例中删除了 api 密钥。当在我的网络浏览器中输入带有我的密钥的网址时,会返回以下错误:

jsonFlickrApi({"stat":"fail", "code":1, "message":"Photoset not found"})

我知道设置的 ID 是正确的,导航到 http://www.flickr.com/photos/23892838@N07/sets/72157623858739780/ 工作正常。

更新

我设法让事情正常运转。这是我修改后的代码,以防其他人试图弄清楚。

function FlickrPhotoSet(){



    //SET API CALL BASED ON INPUT
    var apiCall = "http://api.flickr.com/services/rest/?format=json&method=flickr.photosets.getPhotos&photoset_id=THESET&per_page=10&page=1&api_key=XXX&jsoncallback=?";

    //PRINT API CALL (DEBUG)    
    $("<span>").html(apiCall+"<br>").appendTo("body");

    //SEND API CALL AND RETURN RESULTS TO A FUNCTION    
    $.getJSON(apiCall, function(data){

        //LOOP THROUGH DATA
        $.each(data.photoset.photo, function(i,photo){

        //LINK TO IMAGE SOURCE
        var img_src = "http://farm" + photo.farm + ".static.flickr.com/" + photo.server + "/" + photo.id + "_" + photo.secret + "_" + "s.jpg";

        //LINK TO IMAGE PAGE (REQUIRED BY FLICKR TOS)
        var a_href = "http://www.flickr.com/photos/" + data.photoset.owner + "/" + photo.id + "/";

        //PLACE IMAGE IN IMAGE TAG AND APPEND TO IMAGES DIV 
        $("<img/>").attr("src", img_src).appendTo("#image_block")

        //WRAP IN LINK
        .wrap(("<a href='" + a_href + "'></a>"))
      });

    });


};

I am trying to get a specified set from Flickr and then display the images in that set using the JSON and REST API. Here is the code I am using:

    $.getJSON("http://api.flickr.com/services/rest/?method=flickr.photosets.getPhotos&api_key=xxx&set=72157623858739780&format=json&jsoncallback=?", function(data){
  $.each(data.items, function(i,item){
    $("<img/>").attr("src", item.media.m).appendTo("#images");
  });
});

I removed the api key from the example. The url with my key in it, when typed into my web browser, returns the following error:

jsonFlickrApi({"stat":"fail", "code":1, "message":"Photoset not found"})

I know the set id is correct, as navigating to http://www.flickr.com/photos/23892838@N07/sets/72157623858739780/ works fine.

UPDATE

I managed to get things working. Here is my revised code in case others are trying to figure it out.

function FlickrPhotoSet(){



    //SET API CALL BASED ON INPUT
    var apiCall = "http://api.flickr.com/services/rest/?format=json&method=flickr.photosets.getPhotos&photoset_id=THESET&per_page=10&page=1&api_key=XXX&jsoncallback=?";

    //PRINT API CALL (DEBUG)    
    $("<span>").html(apiCall+"<br>").appendTo("body");

    //SEND API CALL AND RETURN RESULTS TO A FUNCTION    
    $.getJSON(apiCall, function(data){

        //LOOP THROUGH DATA
        $.each(data.photoset.photo, function(i,photo){

        //LINK TO IMAGE SOURCE
        var img_src = "http://farm" + photo.farm + ".static.flickr.com/" + photo.server + "/" + photo.id + "_" + photo.secret + "_" + "s.jpg";

        //LINK TO IMAGE PAGE (REQUIRED BY FLICKR TOS)
        var a_href = "http://www.flickr.com/photos/" + data.photoset.owner + "/" + photo.id + "/";

        //PLACE IMAGE IN IMAGE TAG AND APPEND TO IMAGES DIV 
        $("<img/>").attr("src", img_src).appendTo("#image_block")

        //WRAP IN LINK
        .wrap(("<a href='" + a_href + "'></a>"))
      });

    });


};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三五鸿雁 2024-10-14 04:44:40

从 flickr API 文档 看来您的集URL 中的 参数应为 photoset_id

From the flickr API docs it looks like your set parameter in your URL should be photoset_id.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文