doure8758 2013-03-25 02:47
浏览 35
已采纳

PHP:缓存Facebook Graph API结果

I use Facebook Graph API to fetch my Facebook Fans pages ( 7 Facebook pages in total ) with the following codes :

<?php
header('Content-Type: text/plain; charset=utf-8');
require_once('facebook.php');
$facebook = new Facebook(array(
    'appId'  => 'MY_APP_ID',
    'secret' => 'MY_APP_SECRET',
));

$page_feed_1 = $facebook->api('MY_FB_PAGE_ID_1/feed');
$page_feed_2 = $facebook->api('MY_FB_PAGE_ID_2/feed');
// ... etc
?>

I plan to cache the JSON result to file and the output page ( which displays the contents of the Facebook pages ) loads from the cache files. But what should my loading sequence if I can't use cron job ( yes, my web server is Linux-based ) ?

My current plan is :

  1. When the output page loads, check for cache file, if yes, load the cache file
  2. Use a deferred JavaScript to create an AJAX call to a PHP script to fetch updated contents from Facebook and save to cache file

The problem I can now think of is, when many users visit the output page, the cache will refresh many times. How can I check the last update time of my Facebook page? If the content is the same, the cache won't refresh.

Note: My question is not about parsing JSON / Array in PHP.

  • 写回答

1条回答 默认 最新

  • douchushao7799 2013-10-09 09:37
    关注

    My final solution is to call the Facebook API with a PHP script, which is executed periodically by cronjob.

    The tricky part is to obtain a never-expiring static token from Facebook using Facebook Token Exchange, which saves time from requesting a new token, and skip the need of logging in with a user account.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序