Loading... 最近又有时间来更新一波[123主页](https://123home.page)了,针对采用第三方随机壁纸源经常失效的问题,决定写一个属于自己的随机壁纸源,上传的壁纸全部保存在自己的云存储空间,并采用国内高速 cdn 访问,提升用户体验。 说干就干,经过一番折腾,随机壁纸源功能开发完成,并支持用户自主上传投稿。 然后该写 Bing 每日壁纸了,Bing留有一个每日壁纸的 API 接口:[https://cn.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=zh-CN](https://cn.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=zh-CN),然后获取到一堆 json 数据,images 下有个 url 参数,给它加上 Bing 域名便是每日壁纸的地址了,知道了它的原理,那么 php 代码显而易见: ```php <?php $json_string = file_get_contents('https://cn.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=zh-CN'); $data = json_decode($json_string, true); $url = 'https://cn.bing.com'.$data['images'][0]['url']; header("Location: {$url}"); ?> ``` 这样输出的图片为1920*1080,这是桌面端设备的分辨率,为了进一步提升用户体验,如何搞一个移动端用户访问就显示移动端分辨率的 Bing 每日壁纸呢? 经过搜索抓包,也没有找到获取移动端 Bing 壁纸的接口,然后便尝试分析它的输出链接时发现,Bing 搜索的桌面端壁纸与移动端壁纸的不同之处刚好是我要找的参数部分,也就是分辨率,桌面端壁纸的链接带有1920x1080,而移动端壁纸带有1080x1920,意思就是把链接中的1920x1080改成1080x1920就得到我们要的结果了,尝试了一下果然是这样。那么移动端的 php 代码也就出来了: ```php <?php $json_string = file_get_contents('https://cn.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=zh-CN'); $data = json_decode($json_string, true); $url = 'https://cn.bing.com'.str_replace('1920x1080','1080x1920',$data['images'][0]['url']); header("Location: {$url}"); ?> ``` 如果你懒得写代码,欢迎调用我的 Bing 每日壁纸 api: 自适应:[https://api.123home.page/api.php?category=bing](https://api.123home.page/api.php?category=bing) 桌面端:[https://api.123home.page/pc.php?category=bing](https://api.123home.page/pc.php?category=bing) 移动端:[https://api.123home.page/m.php?category=bing](https://api.123home.page/m.php?category=bing) 更多随机壁纸API请访问 [123主页API](https://api.123home.page/) 获取 一个简洁的浏览器网址主页:[123主页(123home.page)](https://123home.page) 最后修改:2021 年 12 月 19 日 09 : 04 PM © 允许规范转载 赞赏 如果觉得我的文章对你有用,请随意赞赏 赞赏作者 支付宝微信
1 条评论
强啊强啊