javascript - Facebook Graph API 缓存 JSON 响应

标签 javascript php json facebook-graph-api caching

我正在使用 Facebook Graph API 从 Facebook 粉丝页面获取内容,然后将它们显示到网站中。我是这样做的,它正在工作,但不知何故,我的托管服务提供商似乎每隔一段时间就限制我的请求……所以我想缓存响应,并且每 8 小时只请求一个新请求示例。

$data = get_data("https://graph.facebook.com/12345678/posts?access_token=1111112222233333&limit=20&fields=full_picture,link,message,likes,comments&date_format=U");
$result = json_decode($data);

get_data 函数按以下方式使用 CURL:

function get_data($url) {
    $ch = curl_init();
    $timeout = 5;
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    $datos = curl_exec($ch);
    curl_close($ch);
    return $datos;
}

这很好用,我可以输出 JSON 数据响应并根据需要在我的网站中使用它来显示内容。但正如我提到的,在我的托管中,这似乎每 X 次都会失败,我猜是因为我受到了限制。我尝试使用我在 Stackoverflow 上看到的一些代码来缓存响应。但我无法弄清楚如何集成和使用这两种代码。我已设法创建缓存文件,但无法从缓存文件中正确读取并避免向 Facebook 图形 API 发出新请求。

// cache files are created like cache/abcdef123456...
    $cacheFile = 'cache' . DIRECTORY_SEPARATOR . md5($url);

    if (file_exists($cacheFile)) {
        $fh = fopen($cacheFile, 'r');
        $cacheTime = trim(fgets($fh));

        // if data was cached recently, return cached data
        if ($cacheTime > strtotime('-60 minutes')) {
            return fread($fh);
        }

        // else delete cache file
        fclose($fh);
        unlink($cacheFile);
    }

$fh = fopen($cacheFile, 'w');
    fwrite($fh, time() . "\n");
    fwrite($fh, $json);
    fclose($fh);

return $json;

非常感谢您的帮助!

最佳答案

有些想法在尝试构建缓存和缓存实际对象(甚至数组)时可能会派上用场。

函数serializeunserialize允许您获取对象或数组的字符串表示形式,以便您可以将其缓存为纯文本,然后像以前一样从字符串中弹出对象/数组。

filectime这允许您获取文件的最后修改日期,因此当它被创建时,您可以依靠此信息来查看您的缓存是否已过时,就像您尝试实现它一样。

对于整个工作代码,你去:

function get_data($url) {
    /** @var $cache_file is path/to/the/cache/file/based/on/md5/url */
    $cache_file = 'cache' . DIRECTORY_SEPARATOR . md5($url);
    if(file_exists($cache_file)){
        /** 
         * Using the last modification date of the cache file to check its validity 
         */
        if(filectime($cache_file) < strtotime('-60 minutes')){
            unlink($cache_file);
        } else {
            echo 'TRACE -- REMOVE ME -- out of cache';
            /** 
             * unserializing the object on the cache file 
             * so it gets is original "shape" : object, array, ...  
             */
            return unserialize(file_get_contents($cache_file));
        }
    }

    $ch = curl_init();
    $timeout = 5;
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    $data = curl_exec($ch);
    curl_close($ch);

    /** 
     * We actually did the curl call so we need to (re)create the cache file 
     * with the string representation of our curl return we got from serialize 
     */
    file_put_contents($cache_file, serialize($data));

    return $data;
}

PS:请注意,我将您的实际函数 get_data 上的 $datos 变量更改为更常见的 $data.

关于javascript - Facebook Graph API 缓存 JSON 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30692185/

相关文章:

php - 是否有任何安全的方法可以从 bash 脚本作为 www-data 运行 CordovaCLI 命令?

php - 多个文件上传 Angular + Laravel

尝试使用 CodeIgniter 和 MongoDB 驱动程序将集合插入 MongoDB 时出现 PHP 错误

javascript - 转换 Json 的推荐方式/库是什么?

javascript - Jquery JSON 响应处理

javascript - Twitter Bootstrap 关闭按钮不起作用

javascript - 从按钮 Bootstrap 登录表单 div - 单击表单后不保持打开状态

javascript - Selenium C# : Locate element on chart

php - 如何让 json_encode() 与 ISO-8859-1 一起工作 (åäö)

java - 如何从 JSON 数组中检索 JSON 对象