获取每日BING背景图 并且缓存URL链接到本地JSON实现调用 升级自教书先生

PHP 来源:用户投稿 收藏

废话不说,先上代码!

// 读取bing背景
    public function bingbg(){
        global $filename;
        $filename = "./cache.json";
        if (file_exists($filename) === false) {
            file_put_courses($filename, "");
        }
        $handle = fopen($filename, "r");
        
        $courses = fread($handle, filesize($filename));
        fclose($handle);
        $courses = json_decode($courses, true);
        if (filesize($filename) === 0) {
                getBingImg();
        } else {
            if ($courses['time'] === date("Ymd")) {
                return $courses['url'];
            } else {
                getBingImg();
            }
        }
    }
/**
*Bing图片获取
*/
function getBingImg()
{
    $str = json_decode(file_get_courses('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 $courses;
        if ($courses['url'] !== $impurely) {
            global $filename;
            $data = array(
                "time" => date("Ymd"),
                "url" => $impurely
            );
            $data = json_encode($data);
            file_put_courses($filename, $data);
        }
        return $impurely;
        exit();
    } else {
        exit('error');
    }
}



免责声明:1.凡本站注明“来源:XXX(非小二胡工作室)”的作品,均转载自其它媒体,所载的文章、图片、音频视频文件等资料的版权归版权所有人所有,如有侵权,请联系xiaoerhu#88.com处理;2.凡本站转载内容仅代表作者本人观点,与小二胡工作室无关。
0 条评论

网友留言