获取每日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'); } }