Facebook 图形 API 通过分页循环

问题描述 投票:0回答:2

所以我正在使用这个脚本来循环访问给定页面的事件。突然发现已经不行了:(

我有一种感觉,这可能是一个错误,因为如果您选择任何页面,使用 access_token 查看事件,您将无法获取“下一个”分页 URL 的任何数据。例如尝试 apigee.com 中的 https://graph.facebook.com/evenightclub/events

有什么想法吗?

($fid是页面对象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"]); }}
    
php facebook-graph-api pagination facebook-php-sdk
2个回答
7
投票
您的代码对我有用,我所做的唯一一件事就是在将其与现有信息合并之前确保 count($data["data"]) > 0。所以它看起来像这样:

//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; } }}
    

0
投票
我已经使用此方法来获取用户的所有 Facebook 帖子。

$appId = env('FBAPP_ID'); $appSecret = env('FBAPP_SECRET_KEY'); $fb = new Facebook([ 'app_id' => $appId, 'app_secret' => $appSecret, 'default_graph_version' => 'v3.3' ]);

用户允许我的应用程序获取他/她的个人资料后收到的令牌

$access_token = $args['token'];//you need to pass here your token

获取用户详细信息

try { // Returns a `Facebook\FacebookResponse` object $response = $fb->get('/me?fields=id,name,birthday,email,location,picture', $access_token); } catch (Facebook\Exceptions\FacebookResponseException $e) { echo 'Graph returned an error: ' . $e->getMessage(); exit; } catch (Facebook\Exceptions\FacebookSDKException $e) { echo 'Facebook SDK returned an error: ' . $e->getMessage(); exit; } $graphUser = $response->getGraphUser(); $fb_uid = $graphUser['id'];

现在尝试获取用户帖子

try { $fields = "id,message,picture,name,description,type,icon,created_time,from,object_id,attachments,source,full_picture"; $userFeed = $fb->get("/$fb_uid/posts?fields={$fields}&limit=5", $access_token); $feedBody = $userFeed->getDecodedBody(); $feedData = $feedBody["data"]; if (isset($feedBody['paging']) && $feedBody['paging']['next']) { $nextPageData = $this->getNextPageFbPost($feedBody['paging']['next']); while ($nextPageData['paging'] && $nextPageData['paging']['next']) { $nextPageData = $this->getNextPageFbPost($nextPageData['paging']['next']); if (count($nextPageData["data"]) > 0) { $feedData = array_merge($feedData, $nextPageData["data"]); } else { break; } } } } catch (FacebookResponseException $e) { echo 'Facebook returned an error: ' . $e->getMessage(); exit(); } catch (FacebookSDKException $e) { echo 'Facebook SDK returned an error: ' . $e->getMessage(); exit(); } foreach ($feedData as $postData) { print_r($postData); } function getNextPageFbPost($url) { $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_REFERER, ''); curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_TIMEOUT, 10); $raw_xml = curl_exec($curl); $result = json_decode($raw_xml, true); return $result; }
    
© www.soinside.com 2019 - 2024. All rights reserved.