获取每日BING背景图 并且缓存URL链接到本地JSON实现调用
废话不说,先上代码!
- // 读取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');
- }
- }
文章版权声明:本站部分内容系网络转载,如果文章触发到您的利益或版权,请联系本站客服邮箱kefu@fz331.com删除,我们将48小时之内删除。
发表评论