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 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址