Bing 每日壁纸

今日壁纸:

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?php
$filename = "./cache.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) {
// echo "获取\r\n";
getBingImg();
} else {
if ($contents['time'] === date("Ymd")) {
// echo "缓存\r\n";
Header("Location: ".$contents['url']);
} else {
// echo "过期\r\n";
getBingImg();
}
}
function getBingImg() {
$str = file_get_contents('http://cn.bing.com/HPImageArchive.aspx?idx=0&n=1');
if (preg_match("/<url>(.+?)<\/url>/ies", $str, $matches)) {
$imgurl = 'https://cn.bing.com' . $matches[1];
}
if ($imgurl) {
global $contents;
if($contents['url'] !== $imgurl){
global $filename;
$data = array(
"time" => date("Ymd") ,
"url" => $imgurl
);
$data = json_encode($data);
file_put_contents($filename, $data);
}
Header("Location: ".$imgurl);
exit();
} else {
exit('error');
}
}

评论
点击刷新