Facebook图形API循环通过分页 [英] Facebook graph API loop through paging

查看:336
本文介绍了Facebook图形API循环通过分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在使用这个脚本来循环从给定页面的事件。突然间我发现它不再工作了:(

So I was working with this script to loop through the events from a given page. Suddenly I find it doesn't work anymore :(

我有一种感觉可能是一个错误,因为如果你选择任何页面,用access_token查看事件,你无法获取下一个分页网址的任何数据,例如, https://graph.facebook.com/evenightclub/events 在apigee.com

I have a feeling it could be a bug, because if you pick any page, view events with an access_token, you can't get any data back for the "next" paginated URL. e.g. try https://graph.facebook.com/evenightclub/events in apigee.com

任何想法?

($ fid是页面对象id)

($fid is the page object id)

    try {
    $facebook = new Facebook(array(
      'appId'  => '<removed>',
      'secret' => '<removed>',
    ));
    $access_token = $facebook->getAccessToken();

    $events_data = array();
    $offset = 0;
    $limit = 5000;  
    $params = array('access_token' => $access_token);

    //fetch events from Facebook API
    $data = $facebook->api("$fid/events/?limit=$limit&offset=$offset", $params);
    $events_data = array_merge($events_data, $data["data"]);

    //loop through pages to return all results
    while(in_array("paging", $data) && array_key_exists("next", $data["paging"])) {
        $offset += $limit;
        $data = $facebook->api("$fid/events/?limit=$limit&offset=$offset", $params);
        $events_data = array_merge($events_data, $data["data"]);
    }}


推荐答案

您的代码适用于我,我做的唯一的事情是在将它与现有信息合并之前确保count($ data [data])> 0。所以它看起来像这样:

Your code works for me, the only thing that I did is to make sure that count($data["data"]) > 0 before merging it with the existing information. So it looks like this:

//loop through pages to return all results
while(in_array("paging", $data) && array_key_exists("next", $data["paging"])) {
    $offset += $limit;
    $data = $facebook->api("$fid/events/?limit=$limit&offset=$offset", $params);
    // make sure we do not merge with an empty array
    if (count($data["data"]) > 0){
        $events_data = array_merge($events_data, $data["data"]);
    } else {
        // if the data entry is empty, we have reached the end, exit the while loop
        break;
    }
}}

这篇关于Facebook图形API循环通过分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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