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!