php网站上缓存json_decode file_get_contents参考代码
日期:2021-06-14
function Tickers($data) { foreach($data as $item) { echo "<a href=/currencies/" .$item->symbol. '/' . $item->name. ">$item->symbol</a>"; echo "<br>"; echo '<span>$' . $item->price_usd . '</span>'; echo "<br>"; echo "<br>"; } }
function getdata() { $time = 600; //seconds $cache_file = '/path/to/cache.txt'; if (file_exists($cache_file)) { if (time() - filemtime($cache_file) > $time) { // too old , re-fetch $data = file_get_contents('https://api.coinmarketcap.com/v1/ticker/?limit=20'); file_put_contents($cache_file, $data); } else { //data is current } } else { // create cache $data = file_get_contents('https://api.coinmarketcap.com/v1/ticker/?limit=20'); file_put_contents($cache_file, $data); } $data = json_decode(file_get_contents($cache_file)); Tickers($data); } getdata();
本文原创地址:https://www.ecshopok.com/article-806.html
版权所有 © 转载时必须以链接形式注明出处!
觉得本文对您有用,想收藏下来!方法很简单:请点击-〉
我们一直坚持白天工作、晚上熬夜更新资源,付出了巨大的精力和时间,其中的辛酸难以言述。
下一篇:php系统折扣码营销代码_PHP生成促销/优惠/折扣码代码 上一篇:curl请求随机浏览器类型,ip地址,来源网址
暂无评论,来发表一个吧