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条)

报告相同问题?