Facebook Graph API缓存JSON响应

qal*_*iol 15 javascript php json caching facebook-graph-api

我正在使用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);
Run Code Online (Sandbox Code Playgroud)

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;
}
Run Code Online (Sandbox Code Playgroud)

这工作正常,我可以输出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;
Run Code Online (Sandbox Code Playgroud)

非常感谢您的帮助!

β.ε*_*.βε 6

有些人认为在尝试构建缓存和缓存实际对象(甚至是数组)时可能会派上用场.

功能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;
}
Run Code Online (Sandbox Code Playgroud)

PS:请注意,我将$datos实际功能的变量更改get_data为更常见的$data.