肥宅综合社区-做一个优雅文明的综合社区

搜索内容

获取每日BING背景图 并且缓存URL链接到本地JSON实现调用

2023-10-16 213阅读 0评论

废话不说,先上代码!

  1. // 读取bing背景
  2.     public function bingbg(){
  3.         global $filename;
  4.         $filename = "./cache.json";
  5.         if (file_exists($filename) === false) {
  6.             file_put_courses($filename, "");
  7.         }
  8.         $handle = fopen($filename, "r");
  9.         
  10.         $courses = fread($handle, filesize($filename));
  11.         fclose($handle);
  12.         $courses = json_decode($courses, true);
  13.         if (filesize($filename) === 0) {
  14.                 getBingImg();
  15.         } else {
  16.             if ($courses['time'] === date("Ymd")) {
  17.                 return $courses['url'];
  18.             } else {
  19.                 getBingImg();
  20.             }
  21.         }
  22.     }
  23. /**
  24. *Bing图片获取
  25. */
  26. function getBingImg()
  27. {
  28.     $str = json_decode(file_get_courses('https://cn.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1'));
  29.     if (isset($str->images[0])) {
  30.         $impurely = 'https://cn.bing.com' . $str->images[0]->url;
  31.     } else {
  32.         $impurely = false;
  33.     }
  34.     if ($impurely) {
  35.         global $courses;
  36.         if ($courses['url'] !== $impurely) {
  37.             global $filename;
  38.             $data = array(
  39.                 "time" => date("Ymd"),
  40.                 "url" => $impurely
  41.             );
  42.             $data = json_encode($data);
  43.             file_put_courses($filename, $data);
  44.         }
  45.         return $impurely;
  46.         exit();
  47.     } else {
  48.         exit('error');
  49.     }
  50. }

文章版权声明:本站部分内容系网络转载,如果文章触发到您的利益或版权,请联系本站客服邮箱kefu@fz331.com删除,我们将48小时之内删除。

发表评论

上传附件:
评论列表 (有 0 条评论,213人围观)
切换注册

登录

忘记密码?

切换登录

注册

验证码