细心的小伙伴可能发现,文曦博客的背景图怎么一直在更换,其实,我就是分析了微软的一个api,把他用作自己的api。微软是有图片背景api的,在我们开机的时候,是不是发现开机背景图片经常在更换,还有,EDGE浏览器的搜索背景,也是经常改变,那个就是使用的微软api。
本次我写的api程序就是基于EDGE浏览器搜索框背景写出来的。

就是上面这个。
我这边的话是已经写好了PHP自动获取图片,每次刷新都会不一样。不耐烦的可能已经直接用起我的背景图片地址的PHP了。https://vience.dift.cn/bg.php,但是我随时可能关闭,因为为了这个博客网站稳定流量大了就关闭。但你可以根据下面的自己写一个bg.php放在你自己的服务器。

bg.php代码如下,调用的是我自己写的接口(https://api.vience.cn/api/microsoftbg):
<?php/*** 是否是移动端访问* @desc 判断是否是移动端进行访问* @方法三:判断是否有HTTP_USER_AGENT信息是否是手机发送的客户端标志,有则一定是移动设备。* @return bool* $Author: Zhihua_W*/function isMobile (){if (isset ($_SERVER['HTTP_USER_AGENT'])) {$clientkeywords = array ('nokia', 'sony','ericsson','mot','samsung','htc','sgh','lg','sharp','sie-','philips','panasonic','alcatel','lenovo','iphone','ipod','blackberry','meizu','android','netfront','symbian','ucweb','windowsce','palm','operamini','operamobi','openwave','nexusone','cldc','midp','wap','mobile');// 从HTTP_USER_AGENT中查找手机浏览器的关键字if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT']))){return true;} else {return false;}} else {return false;}}function bgimg(){$stream_opts = ["ssl" => ["verify_peer"=>false,"verify_peer_name"=>false,]];$data = file_get_contents("https://api.vience.cn/api/microsoftbg",false, stream_context_create($stream_opts));$datajson = json_decode($data, JSON_UNESCAPED_UNICODE);if(isMobile()){$img = $datajson["data"]["m"];}else{$img = $datajson["data"]["pc"];}return $img;}echo bgimg();//重定向浏览器header("Location: ".bgimg());//确保重定向后,后续代码不会被执行exit;?><?php /** * 是否是移动端访问 * @desc 判断是否是移动端进行访问 * @方法三:判断是否有HTTP_USER_AGENT信息是否是手机发送的客户端标志,有则一定是移动设备。 * @return bool * $Author: Zhihua_W */ function isMobile () { if (isset ($_SERVER['HTTP_USER_AGENT'])) { $clientkeywords = array ('nokia', 'sony','ericsson','mot', 'samsung','htc','sgh','lg','sharp', 'sie-','philips','panasonic','alcatel', 'lenovo','iphone','ipod','blackberry', 'meizu','android','netfront','symbian', 'ucweb','windowsce','palm','operamini', 'operamobi','openwave','nexusone','cldc', 'midp','wap','mobile' ); // 从HTTP_USER_AGENT中查找手机浏览器的关键字 if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT']))){ return true; } else { return false; } } else { return false; } } function bgimg(){ $stream_opts = [ "ssl" => [ "verify_peer"=>false, "verify_peer_name"=>false, ] ]; $data = file_get_contents("https://api.vience.cn/api/microsoftbg",false, stream_context_create($stream_opts)); $datajson = json_decode($data, JSON_UNESCAPED_UNICODE); if(isMobile()){ $img = $datajson["data"]["m"]; }else{ $img = $datajson["data"]["pc"]; } return $img; } echo bgimg(); //重定向浏览器 header("Location: ".bgimg()); //确保重定向后,后续代码不会被执行 exit; ?><?php /** * 是否是移动端访问 * @desc 判断是否是移动端进行访问 * @方法三:判断是否有HTTP_USER_AGENT信息是否是手机发送的客户端标志,有则一定是移动设备。 * @return bool * $Author: Zhihua_W */ function isMobile () { if (isset ($_SERVER['HTTP_USER_AGENT'])) { $clientkeywords = array ('nokia', 'sony','ericsson','mot', 'samsung','htc','sgh','lg','sharp', 'sie-','philips','panasonic','alcatel', 'lenovo','iphone','ipod','blackberry', 'meizu','android','netfront','symbian', 'ucweb','windowsce','palm','operamini', 'operamobi','openwave','nexusone','cldc', 'midp','wap','mobile' ); // 从HTTP_USER_AGENT中查找手机浏览器的关键字 if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT']))){ return true; } else { return false; } } else { return false; } } function bgimg(){ $stream_opts = [ "ssl" => [ "verify_peer"=>false, "verify_peer_name"=>false, ] ]; $data = file_get_contents("https://api.vience.cn/api/microsoftbg",false, stream_context_create($stream_opts)); $datajson = json_decode($data, JSON_UNESCAPED_UNICODE); if(isMobile()){ $img = $datajson["data"]["m"]; }else{ $img = $datajson["data"]["pc"]; } return $img; } echo bgimg(); //重定向浏览器 header("Location: ".bgimg()); //确保重定向后,后续代码不会被执行 exit; ?>
当然,如果你要图片描述,也是提供的,如图。

© 版权声明
THE END
暂无评论内容