如何在PHP的Json请求中创建缓存文件

时间:2017-02-21 11:16:01

标签: php json caching

我尝试为快速JSON响应创建缓存文件但是我遇到了一些问题我在PHP文件中获得了此代码但是我无法理解如何为(function (global) { System.config({ paths: { 'npm:': 'Scripts/Lib/Angular/' }, map: { app: 'Application', '@angular/core': 'npm:core.umd.js', '@angular/common': 'npm:common.umd.js', '@angular/compiler': 'npm:compiler.umd.js', '@angular/platform-browser': 'npm:platform-browser.umd.js', '@angular/platform-browser-dynamic': 'npm:platform-browser-dynamic.umd.js', '@angular/http': 'npm:http.umd.js', '@angular/router': 'npm:router.umd.js', '@angular/forms': 'npm:forms.umd.js', 'rxjs': 'npm:rxjs' }, packages: { app: { main: './Startup.js', defaultExtension: 'js' }, rxjs: { defaultExtension: 'js' } } }); })(this); JSON创建此代码任何JSON响应的文件我有很多技能请任何人都可以帮我解决这个问题

以下是我正在尝试的代码

$url

提前致谢

1 个答案:

答案 0 :(得分:1)

您的代码存在多个错误。您可以简单地使用file_put_contents将内容添加到缓存文件。

尝试以下示例

$id = 'DU6IdS2gVog';
$cache_path = 'cache/';
$filename = $cache_path.md5($id);

if( file_exists($filename) && ( time() - 84600 < filemtime($filename) ) )
{
    $data = json_decode(file_get_contents($filename), true);
}
else
{
    $data = data_get_curl('https://www.googleapis.com/youtube/v3/videos?key={API_KEY}&fields=items(snippet(title%2Cdescription%2Ctags))&part=snippet&id='.$id);
    file_put_contents($filename, $data);
    $data = json_decode($data, true);
}