0
votes

I am developing a facebook app with PHP (SDK v5). I need to get the posts of a page in which the user has admin privilleges and for each post to count it's likes/comments/shares.

Using the Graph API i ended up with this solution:

$fb = new Facebook\Facebook([
'app_id' => 'XXX',
'app_secret' => 'XXX',
'default_graph_version' => 'v2.7',]);

$pageId = //find the page id
$accessToken = //and the access token

$response = $fb->get('/'.$pageId.'/feed', (string) $accessToken);
$response = $response->getDecodedBody();
$pagePosts = $response['data'];
foreach ($pagePosts as $post) {

    //query for the likes and count them
    $likesResponse = $fb->get('/'.$post['id'].'/likes', (string) $accessToken);
    $likesObject = $likesResponse->getDecodedBody();
    echo 'likes count: '.count($likesObject['data']).'<br>';

    //comments and shares similar to likes
}

I think that the solution above is far from perfect since for every facebook post three additional calls will be executed (likes, comment, shares). The performance and sclability of this is terrible.

Now for a performance boost, i was planning to use FQL but i stumbled uppon this comment from the facebook FQL documentation:

As of August 8, 2016, FQL will no longer be available and cannot be queried.


So my question is:

Does anyone know another way to retrieve likes/comments/shares efficiently, or even a way to improve the performance of the curent solution?

1
And asking for all likes and counting them yourself is really bad of course. Set the limit to 0, and request the summary. developers.facebook.com/docs/graph-api/reference/v2.7/object/… - CBroe
Thank you for your answers. So i will still loop through all posts and request for likes, comments, and shares separately? The difference is that i will request for the summary that will decrease the volume of information? - Mario
No, you can ask for the information directly via the /feeds endpoint; see syntax example here, stackoverflow.com/a/36479531/1427878 The loop should become unnecessary. Use Graph API Explorer to try & test stuff before putting it into code, developers.facebook.com/tools/explorer - CBroe
Just tested it and it works nicely. Thank you very much! I will post an answer with what i have ended up with by following your comments. If you want, you can post an answer too and i will accept it. Thank you again for your help. - Mario

1 Answers

0
votes

Following @CBroe suggestions i found a solution. The solution requires a single call to the Graph API which is a big improvment from the (3*posts)+1 calls:

$fb = new Facebook\Facebook([
  'app_id' => 'XXX',
  'app_secret' => 'XXX',
  'default_graph_version' => 'v2.7',]);

$pageId = //find the page id
$accessToken = //and the access token

$response = $fb->get('/'.$pageId.'/feed?fields=likes.summary(1).limit(0),comments.summary(1).limit(0), ....', (string) $accessToken);
$response = $response->getDecodedBody();
$pagePosts = $response['data'];

foreach ($pagePosts as $post) {
  $likesObject = $post['likes'];
  $likesCount = $likesObject['summary']['total_count'];

  $commentsObject = $post['comments'];
  $commentsCount = $commentsObject['summary']['total_count'];
}