I am calling Facebook API to retrieve my fan page's like count on my Wordpress blog. It is worki开发者_如何学运维ng fine (I retrieved the XML and parsed it with file_get_contents.) Now the problem is that the API is called at every page load and to also file_get_contents() is rather slow a method. I want to call the API only once per hour and keep the data in cache to reduce load times.
I don't know hot to go about it? Is that even possible? Help.
It's a little low-tech but storing the "likes" with a timestamp in a text file should do the trick. Something like this should work (PHP5):
$store = 'likes.txt'; // make sure this file exists, empty is fine to start with
list($likes, $stamp) = explode('|', file_get_contents($store));
if ((time() - $stamp) > 3600){
// use your own page's name here instead of "php"
$fbook = json_decode(file_get_contents("https://graph.facebook.com/php"));
$likes = $fbook->{'likes'};
file_put_contents($store, "$likes|".time());
}
echo $likes;
精彩评论