I'm trying to write a simple tracking system for several facebook campaigns we run (seven campaigns at the moment). A key feature in this system is being able to see all the like counts of those pages in real time, so i wrote a simple php code to get the like count for each page from the graph api and I call it with ajax every few seconds. The problem is the slow response from facebook, it takes around 2 seconds to get a response and can get up to 20 (!!!) seconds. Here is the function I wrote to get the like count:
function getlikes($pageid){
echo json_decode(file_get_contents("http://graph.facebook.com/" . $pageid . "?fields=likes"))->likes;
};
Is there any way around it? or the graph-api is just slow and i'll have to deal with it with some elegant solution?
Thanks.
i advise you to cache like counts somewhere on your server to make AJAX requests fast (and it'd be better if there are more than one client watching counts). on server side there should be a cron job updating the like count cache.
to speedup getting counts from Facebook you can make all requests to it in parallel with curl multi-requests. if you are interested, take a look at the rolling curl library