【问题标题】:Efficiently retrieve likes/shares/comments on facebook posts (PHP)有效地检索 Facebook 帖子上的喜欢/分享/评论 (PHP)
【发布时间】:2016-09-14 14:14:41
【问题描述】:

我正在使用 PHP (SDK v5) 开发一个 facebook 应用程序。我需要获取用户具有管理员权限的页面的帖子,并为每个帖子计算它的喜欢/cmets/shares。

使用 Graph API 我最终得到了这个解决方案:

$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
}

我认为上述解决方案远非完美,因为对于每个 Facebook 帖子,都会执行三个额外的调用(喜欢、评论、分享)。它的性能和可扩展性很糟糕。

现在为了提高性能,我计划使用 FQL,但我偶然发现了 facebook FQL 文档中的这条评论:

自 2016 年 8 月 8 日起,FQL 将不再可用且无法使用 查询。


所以我的问题是:

有没有人知道另一种有效检索喜欢/cmets/shares 的方法,甚至是提高当前解决方案性能的方法?

【问题讨论】:

标签: php facebook facebook-graph-api facebook-php-sdk


【解决方案1】:

按照@CBroe 的建议,我找到了解决方案。该解决方案需要对 Graph API 进行一次调用,这是对 (3*posts)+1 调用的一大改进:

$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'];
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-01
    • 2013-10-04
    相关资源
    最近更新 更多