在Facebook Graph Api的数据上运行for循环 [英] Running a for loop on data from Facebook Graph Api

查看:118
本文介绍了在Facebook Graph Api的数据上运行for循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我妈妈的婚礼网站上动态显示照片,她在Facebook上添加了她的照片专辑,但我无法让图像渲染和循环。我的获取请求正在工作,目前正在拉入图片链接和相册中每张照片的ID。这是返回的json的一个例子。

I am trying to dynamically show photos on my moms wedding website that she adds to her photos albums on Facebook but I am having trouble getting the images to render and loop. My get request is working and currently pulling in the image link and the id of each photo in the album. This is an example of the json that is returned.

{
 "photos": {
  "data": [
     {
        "picture": "https://scontent.xx.fbcdn.net/v/t1.0-0/s130x130/13879222_1235179076493734_1631898598554522774_n.jpg?oh=e6d74e7731f374be63a378e99ef3a88e&oe=58111449",
        "id": "1235179076493734"
     }

这是我的代码到目前为止

Here's my code so far

$.getJSON('https://graph.facebook.com/102060453138941?fields=photos{picture}&access_token=xxx', 
function(fbResults) {

        for(var i =0; i < fbResults; i++) {

            $('.gallery').append(

            '<img class="" src="https://graph.facebook.com/'+ this.id +'/picture" />'

            );


        }



    });


推荐答案

尝试这样:

for(var i = 0; i < fbResults.photos.data.length; i++) {
    $('.gallery').append(
        '<img class="" src="https://graph.facebook.com/'+ fbResults.photos.data[i].id +'/picture" />'
    );
}

Btw,您只能使用API​​获取25个条目。您可以使用limit参数设置限制,也可以使用分页: https://developers.facebook.com/docs/graph-api/using-graph-api/#paging

Btw, you will only get 25 entries with the API. You can set a limit with the limit parameter or you can use paging: https://developers.facebook.com/docs/graph-api/using-graph-api/#paging

如果您需要获取所有我建议您使用带有递归函数的分页。

If you need to get all entries at once, i suggest using paging with a recursive function.

Btw,您需要将每个行添加用户令牌,这是一个非常糟糕的主意。整个代码应该更好的去服务器,你应该实际下载照片并缓存它们。只是说。

Btw, you would need to add the User Token to each line, which is a very bad idea. The whole code should better go to the server and you should actually download the photos and cache them. Just saying.

这篇关于在Facebook Graph Api的数据上运行for循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆