问题
So I was working with this script to loop through the events from a given page. Suddenly I find it doesn't work anymore :(
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
Any ideas?
($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"]);
}}
回答1:
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;
}
}}
回答2:
I have used this method to get all Facebook posts by user.
$appId = env('FBAPP_ID');
$appSecret = env('FBAPP_SECRET_KEY');
$fb = new Facebook([
'app_id' => $appId,
'app_secret' => $appSecret,
'default_graph_version' => 'v3.3'
]);
Token received after user allow my app to get his/her profile
$access_token = $args['token'];//you need to pass here your token
Get user detail
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'];
Now try to get user posts
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;
}
来源:https://stackoverflow.com/questions/9576211/facebook-graph-api-loop-through-paging