PHP 获取每日BING图 并且缓存URL链接到本地JSON

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

<?php
$filename = "./bing.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');
    }
}
?>

全能资源库丨本站资源除特殊声明外,均来自于网络收集整理仅供学习研究,如需商业使用请获得作者商业授权,如果侵犯了您的合法权益,请联系我们的投诉邮箱:qinglikf@163.com,我们将在24小时内删除!如有其它疑问请联系在线客服(微信公众号:轻狸团队 丨菜单栏人工客服)特别注意:访问本站即同意本站《免责声明&版权声明》,本站原创内容未经授权禁止转载!
全能资源库 » PHP 获取每日BING图 并且缓存URL链接到本地JSON

提供最优质的资源集合,会员畅享0元下载!

立即开通 了解详情