php 获取Bing首页壁纸并保存至本地

 1 <?php
 2 
 3 define('WALLPAPER_PATH','C:/xxx/xxx'); //本地目录
 4 
 5 class BingPicture{
 6     
 7     private $content = "";
 8     private $imgurl = "";
 9     
10     public function __construct() {
11         $this->_getWallpaperUrl();
12     }
13     
14     //获取壁纸
15     public function _getWallpaperUrl(){
16         if (!function_exists('file_get_contents')) 
17             return false;
18         
19         $this->content=file_get_contents('https://cn.bing.com/HPImageArchive.aspx?idx=0&n=1');
20         
21         if (preg_match("/<url>(.+?)<\/url>/ies", $this->content, $matches)) {
22             $this->imgurl='https://cn.bing.com'.$matches[1];
23         }
24     }
25     
26     //获取壁纸信息
27     public function getWallpaperInfo(){
28         if (preg_match("/<copyright>(.+?)<\/copyright>/ies", $this->content, $matches)) {
29             $imgcopyright=$matches[1];
30         }
31         
32         if($imgcopyright){
33             return $imgcopyright;
34         }
35     }
36     
37     //保存壁纸    
38     public function saveWallpaper() {
39         
40         if(!file_exists(WALLPAPER_PATH)){
41             mkdir(WALLPAPER_PATH, 0777);
42         }
43         
44         $url = $this->imgurl;
45         $pic = 'Bing'.date("Ymd").'.jpg';
46         $file = WALLPAPER_PATH.'/'.$pic;
47         
48     if ($url == "")
49         return false;
50             
51         if(file_exists($file))
52                 return true;
53 
54     ob_start ();
55     readfile ( $url );
56     $img = ob_get_contents ();
57     ob_end_clean ();
58     $size = strlen ( $img );
59         
60         $fp2 = @fopen ( WALLPAPER_PATH . DIRECTORY_SEPARATOR . $pic, "a" );
61         fwrite ( $fp2, $img );
62         fclose ( $fp2 );
63     }
64     
65 }

以及创建任务计划定时执行脚本

 1 @echo off
 2 
 3 rem 定时获取并保存Bing壁纸脚本
 4 
 5 cd /d F:
 6 
 7 rem php.exe所在目录
 8 set PHP_PATH=F:\xxx
 9 
10 rem 脚本所在目录
11 start %PHP_PATH% -q F:\xxxx\xxx.php
12 
13 exit

 

posted @ 2020-03-20 15:29  Yan327  阅读(367)  评论(0编辑  收藏  举报