Bing 图片相信每位使用过必应搜索的小伙伴都了解,网上也有很多关于如何将 Bing 图片设置为电脑壁纸的教程,甚至微软专门推出过一款 Bing 桌面的应用方便大家将必应的美图设置为电脑桌面壁纸。因为 Bing 图片每日都不同并且每个图片后面都伴随一个小故事,受到了很多小伙伴的欢迎。 ![][1] [1]: https://www.feihong.me/usr/uploads/2021/08/2638954724.jpg 那么,如果可以把 Bing 的每日图片设置为博客背景会不会很酷炫呢? ###接口 通过抓包,我们可以发现这个链接 https://cn.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1 目前,国内版和国际版的每日图片内容显示一致,如果你想获取国际版的每日图片,只需将网址中的 “cn” 替换为 “www” 即可。 这里需要注意,链接中 idx=0 表示是显示当天的时间,如果要显示昨天的就将 idx=0 改为 idx=1,以此类推。必应每日图片支持查看历史 15 天以内的图片。 抓包显示结果: ``` {"images":[{"startdate":"20180917","fullstartdate":"201809171600","enddate":"20180918","url":"/az/hprichbg/rb/CalidrisCanutus_ZH-CN10519054459_1920x1080.jpg","urlbase":"/az/hprichbg/rb/CalidrisCanutus_ZH-CN10519054459","copyright":"在设得兰群岛觅食的红腹滨鹬,苏格兰 (© Andrew Parkinson/Minden Pictures)","copyrightlink":"http://www.bing.com/search?q=%E7%BA%A2%E8%85%B9%E6%BB%A8%E9%B9%AC&form=hpcapt&mkt=zh-cn","title":"","quiz":"/search?q=Bing+homepage+quiz&filters=WQOskey:%22HPQuiz_20180917_CalidrisCanutus%22&FORM=HPQUIZ","wp":true,"hsh":"0a7f98e36e11f76ca5baea28f6ff86ac","drk":1,"top":1,"bot":1,"hs":[]}],"tooltips":{"loading":"正在加载...","previous":"上一个图像","next":"下一个图像","walle":"此图片不能下载用作壁纸。","walls":"下载今日美图。仅限用作桌面壁纸。"}} ``` ###获取图片 有了接口我们就可以直接利用正则表达式去匹配相关字符串了。 以下是将必应每日图片通过 php 直接输出分辨率为 1366X768 图片的代码: ```php (.+?)<\/url>/", $str, $matches)) { $imgurl='https://cn.bing.com'.$matches[1]; } if ($imgurl) { header('Content-Type: image/JPEG'); @ob_end_clean(); @readfile($imgurl); @flush(); @ob_flush(); exit(); } else { exit('error'); } ?> ``` 考虑到直接输出图片会占用一定的主机带宽,我们也可以将 Bing 图片做 302 跳转。以下是通过 php 将输出图片 302 定向到 Bing 的代码: ``` (.+?)<\/url>/", $str, $matches)) { $imgurl='https://cn.bing.com'.$matches[1]; } if ($imgurl) { header("Location: $imgurl"); //输出302跳转 } else { exit('error'); } ?> ``` 根据需要复制上面两段代码中的任意 1 段,将文件保存为 bg.php 或任意名称.php 上传到你的服务器。访问 http://***.com/bg.php 就可以看到必应每日图片了。 考虑到有些网友的需求,增加了输出 1920X1080 高清图片的 php 代码。 ``` (.+?)<\/urlBase>/", $str, $matches)) { $imgurl='https://cn.bing.com'.$matches[1].'_1920x1080.jpg'; } if ($imgurl) { header('Content-Type: image/JPEG'); @ob_end_clean(); @readfile($imgurl); @flush(); @ob_flush(); exit(); } else { exit('error'); } ?> ``` ###效果展示 http://nav.feihong.me Loading... <div class="tip share">请注意,本文编写于 1757 天前,最后修改于 1757 天前,其中某些信息可能已经过时。</div> Bing 图片相信每位使用过必应搜索的小伙伴都了解,网上也有很多关于如何将 Bing 图片设置为电脑壁纸的教程,甚至微软专门推出过一款 Bing 桌面的应用方便大家将必应的美图设置为电脑桌面壁纸。因为 Bing 图片每日都不同并且每个图片后面都伴随一个小故事,受到了很多小伙伴的欢迎。 ![][1] [1]: https://www.feihong.me/usr/uploads/2021/08/2638954724.jpg 那么,如果可以把 Bing 的每日图片设置为博客背景会不会很酷炫呢? ###接口 通过抓包,我们可以发现这个链接 https://cn.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1 目前,国内版和国际版的每日图片内容显示一致,如果你想获取国际版的每日图片,只需将网址中的 “cn” 替换为 “www” 即可。 这里需要注意,链接中 idx=0 表示是显示当天的时间,如果要显示昨天的就将 idx=0 改为 idx=1,以此类推。必应每日图片支持查看历史 15 天以内的图片。 抓包显示结果: ``` {"images":[{"startdate":"20180917","fullstartdate":"201809171600","enddate":"20180918","url":"/az/hprichbg/rb/CalidrisCanutus_ZH-CN10519054459_1920x1080.jpg","urlbase":"/az/hprichbg/rb/CalidrisCanutus_ZH-CN10519054459","copyright":"在设得兰群岛觅食的红腹滨鹬,苏格兰 (© Andrew Parkinson/Minden Pictures)","copyrightlink":"http://www.bing.com/search?q=%E7%BA%A2%E8%85%B9%E6%BB%A8%E9%B9%AC&form=hpcapt&mkt=zh-cn","title":"","quiz":"/search?q=Bing+homepage+quiz&filters=WQOskey:%22HPQuiz_20180917_CalidrisCanutus%22&FORM=HPQUIZ","wp":true,"hsh":"0a7f98e36e11f76ca5baea28f6ff86ac","drk":1,"top":1,"bot":1,"hs":[]}],"tooltips":{"loading":"正在加载...","previous":"上一个图像","next":"下一个图像","walle":"此图片不能下载用作壁纸。","walls":"下载今日美图。仅限用作桌面壁纸。"}} ``` ###获取图片 有了接口我们就可以直接利用正则表达式去匹配相关字符串了。 以下是将必应每日图片通过 php 直接输出分辨率为 1366X768 图片的代码: ```php <?php $str=file_get_contents('https://cn.bing.com/HPImageArchive.aspx?idx=0&n=1'); if (preg_match("/<url>(.+?)<\/url>/", $str, $matches)) { $imgurl='https://cn.bing.com'.$matches[1]; } if ($imgurl) { header('Content-Type: image/JPEG'); @ob_end_clean(); @readfile($imgurl); @flush(); @ob_flush(); exit(); } else { exit('error'); } ?> ``` 考虑到直接输出图片会占用一定的主机带宽,我们也可以将 Bing 图片做 302 跳转。以下是通过 php 将输出图片 302 定向到 Bing 的代码: ``` <?php $str=file_get_contents('https://cn.bing.com/HPImageArchive.aspx?idx=0&n=1'); if (preg_match("/<url>(.+?)<\/url>/", $str, $matches)) { $imgurl='https://cn.bing.com'.$matches[1]; } if ($imgurl) { header("Location: $imgurl"); //输出302跳转 } else { exit('error'); } ?> ``` 根据需要复制上面两段代码中的任意 1 段,将文件保存为 bg.php 或任意名称.php 上传到你的服务器。访问 http://***.com/bg.php 就可以看到必应每日图片了。 考虑到有些网友的需求,增加了输出 1920X1080 高清图片的 php 代码。 ``` <?php $str=file_get_contents('https://cn.bing.com/HPImageArchive.aspx?idx=0&n=1'); if (preg_match("/<urlBase>(.+?)<\/urlBase>/", $str, $matches)) { $imgurl='https://cn.bing.com'.$matches[1].'_1920x1080.jpg'; } if ($imgurl) { header('Content-Type: image/JPEG'); @ob_end_clean(); @readfile($imgurl); @flush(); @ob_flush(); exit(); } else { exit('error'); } ?> ``` ###效果展示 http://nav.feihong.me 最后修改:2021 年 08 月 29 日 © 允许规范转载 打赏 赞赏作者 支付宝微信 赞 如果觉得我的文章对你有用,请随意赞赏