PHP 获取每日BING图 并且缓存URL链接到本地JSON-将获取的BING图链接进行缓存,减少服务器负担,提升访问速度。 ?php $filename = ./cache.json; if (file_exists($filename...

建站技术分享网

建站技术分享网
首页>> php >>PHP 获取每日BING图 并且缓存URL链接到本地JSON

将获取的BING图链接进行缓存,减少服务器负担,提升访问速度。

<?php $filename = "./cache.json"; if (file_exists($filename) === false) {
    file_put_contents($filename, "");
}
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);
$contents = json_decode($contents, true); if (filesize($filename) === 0) {
    getBingImg();
} else { if ($contents['time'] === date("Ymd")) {
        Header("Location: " . $contents['url']);
    } else {
        getBingImg();
    }
} function getBingImg() {
    $str = json_decode(file_get_contents('https://cn.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1')); if (isset($str->images[0])) {
        $impurely = 'https://cn.bing.com' . $str->images[0]->url;
    } else {
        $impurely = false;
    } if ($impurely) { global $contents; if ($contents['url'] !== $impurely) { global $filename;
            $data = array( "time" => date("Ymd"), "url" => $impurely
            );
            $data = json_encode($data);
            file_put_contents($filename, $data);
        }
        Header("Location: " . $impurely); exit();
    } else { exit('error');
    }
} ?>

×

感谢您的支持,我们会一直保持!

扫码支持
请土豪扫码随意打赏

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

打赏作者
版权所有,转载注意明处:FEIBA分享博客 » PHP 获取每日BING图 并且缓存URL链接到本地JSON

发表评论

路人甲

网友评论(0)