dragonlew9876 2011-11-03 00:53
浏览 30
已采纳

从Facebook图表api快速获取信息?

I have a site with posts, and I am implementing Facebook comments on them. All the posts will be in one page, and below each a 'Comments(3)' link. Once this is clicked it will go to the comments page of this post, showing the Facebook plugin.

My problem is that obtaining that 3 from 'comments(3)' is taking a while... How can I optimize this? This is how the page where the comments are looks like: https://graph.facebook.com/?ids=http://example.com/

On the page where I get the comments, this is what I have for each post:

echo $postsClass->getCommentsCount($post['id'])

And then this is how the getCommentsCount function looks like:

public function getCommentsCount($postId) {
    $commentsCount = 0;
    $url = 'http://myurl.com?post=' . $postId;
    $html = file_get_contents('http://graph.facebook.com/?ids=' . $url);
    $comments = json_decode($html, true);
    if (array_key_exists('comments', $comments[$url])) {
        $commentsCount = $comments[$url]['comments'];
    }

    return $commentsCount;
}

It seems to take around 3 or 4 seconds to load the comments for 6 posts... Any ideas on how to improve this?

Thank you!

  • 写回答

5条回答 默认 最新

  • dongpo7467 2011-11-06 06:39
    关注

    The Facebook Graph API supports Real-time Updates via a subscription system.

    I'm not sure if there is a delay in the change notice delivery; it is guaranteed to be more efficient, however. As you receive the updates, simply cache the results in your your database for quick retrieval. Be warned that it can be quite a heavy load if you are subscribing to many quickly changing objects.

    Another alternative is to schedule your server to poll the comments every so often (every few minutes?) and store the information.

    Or, you could present the page and then have the user's browser query the comment count via AJAX. The user could retrieve the information via your server, or directly from Facebook if they has access to the object in context.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号