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 pnpm 下载element-plus
  • ¥15 解决编写PyDracula时遇到的问题
  • ¥15 有没有人能解决下这个问题吗,本人不会编程
  • ¥15 plotBAPC画图出错
  • ¥30 关于#opencv#的问题:使用大疆无人机拍摄水稻田间图像,拼接成tif图片,用什么方法可以识别并框选出水稻作物行
  • ¥15 Python卡尔曼滤波融合
  • ¥20 iOS绕地区网络检测
  • ¥15 python验证码滑块图像识别
  • ¥15 根据背景及设计要求撰写设计报告
  • ¥20 能提供一下思路或者代码吗