如何搭建自己的必应每日一图API

必应官方的每日一图api为 https://cn.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1

通过直接访问这个api,可以得到如下的信息:

https://cn.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1

{"images":[{"startdate":"20220301","fullstartdate":"202203011600","enddate":"20220302","url":"/th?id=OHR.MoonlightRainier_ZH-CN6263832605_1920x1080.jpg&rf=LaDigue_1920x1080.jpg&pid=hp","urlbase":"/th?id=OHR.MoonlightRainier_ZH-CN6263832605","copyright":"雷尼尔山上空的银河星系,美国华盛顿州 (© Brad Goldpaint/Cavan)","copyrightlink":"https://www.bing.com/search?q=%E9%9B%B7%E5%B0%BC%E5%B0%94%E5%B1%B1&form=hpcapt&mkt=zh-cn","title":"123岁生日快乐!","quiz":"/search?q=Bing+homepage+quiz&filters=WQOskey:%22HPQuiz_20220301_MoonlightRainier%22&FORM=HPQUIZ","wp":true,"hsh":"2aef55596c6eacaf2ea1dd86ea9966fd","drk":1,"top":1,"bot":1,"hs":[]}],"tooltips":{"loading":"正在加载...","previous":"上一个图像","next":"下一个图像","walle":"此图片不能下载用作壁纸。","walls":"下载今日美图。仅限用作桌面壁纸。"}}

其中,这段就是真正的图片url:

/th?id=OHR.MoonlightRainier_ZH-CN6263832605_1920x1080.jpg&rf=LaDigue_1920x1080.jpg&pid=hp

把它加到https://cn.bing.com后即可得到真正的每日一图url。

th (1920×1080) (bing.com)

最后,我们写个简单的php程序来做这件事吧:

<?php
    $str = file_get_contents('https://cn.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1');  //读取必应api
    $str = json_decode($str,true);  //接受JSON 编码的字符串并且把它转换为 PHP变量
    $imgurl = 'https://cn.bing.com'.$str['images'][0]['url'];  //获取图片url
    header("Location: {$imgurl}");  // 跳转至目标图像
?>

然后,访问它就会得到每天的最新必应每日一图了,附上本站的api地址:

https://api.yanqishui.work/bing_daily_pic.php

阅读剩余
THE END