在PHP中每小时只调用一次Facebook API

时间:2011-03-15 11:52:59

标签: facebook facebook-graph-api facebook-fql

我正在调用Facebook API来检索我的粉丝页面,就像在我的Wordpress博客上计算一样。它运行正常(我检索了XML并使用file_get_contents对其进行了解析。)现在问题是在每次加载页面时都会调用API,而且file_get_contents()的调用速度相当慢。我想每小时只调用一次API并将数据保存在缓存中以减少加载时间。

我不知道热的事吗?这甚至可能吗?帮助

1 个答案:

答案 0 :(得分:0)

这有点低技术但是在文本文件中存储带有时间戳的“喜欢”应该可以解决问题。这样的东西应该有效(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;