此API只支持https调用,所有参数均仅适用于以 GET 方式进行请求,可直接插入img标签中!
调用参数
参数代码 | 参数含义 | 可用参数 |
---|---|---|
rand | 是否随机显示最近8天内的图片 | true or false |
day | 显示指定的最近图片 | -1,0,1,2,3,4,5,6,7(0为今天,-1为昨天) |
size | 指定获取图片大小 | 详见下方可用分辨率 |
info | 获取图片基础信息(json格式) | true or false |
以上所有参数均非必要,默认参数为rand=false,day=0,size=1920×1080,info=false
可用分辨率
1920×1080,1366×768,1280×768,1024×768,800×600,800×480,768×1280,720×1280,640×480,480×800,400×240,320×240,240×320
调用示例
默认调用
https://xx.xx/api/bing
不带任何参数调用,显示必应当天图片。
Info调用
https://xx.xx/api/bing?info=true
返回值:
{title:庐山冬季美景,中国江西省 (© silkwayrain/Getty Images),url:https://www.bing.com/th?id=OHR.LiDong2022_ZH-CN9929478283_1920x1080.jpg,link:https://www.bing.com/search?q=%E7%AB%8B%E5%86%AC&form=hpcapt&mkt=zh-cn,time:20221106}
教程和API代码
首先在网站根目录新建一个php文件,例如:index.php,然后粘贴如下代码:
<?php
//判断是否随机调用
if ($_GET['rand']==='true') {
$gettime = rand(-1,7);
}else{
//若不为随机调用则判断是否指定日期
$gettimebase = $_GET['day'];
if (empty($gettimebase)) {
$gettime = 0;
}else{
$gettime = $gettimebase;
}
}
//获取Bing Json信息
$json_string = file_get_contents('https://www.bing.com/HPImageArchive.aspx?format=js&idx='.$gettime.'&n=1');
//转换为PHP数组
$data = json_decode($json_string);
//提取基础url
$imgurlbase = "https://www.bing.com".$data->{"images"}[0]->{"urlbase"};
//判断是否指定图片大小
$imgsizebase = $_GET['size'];
if (empty($imgsizebase)){
$imgsize = "1920x1080";
}else{
$imgsize = $imgsizebase;
}
//建立完整url
$imgurl = $imgurlbase."_".$imgsize.".jpg";
//获取其他信息
$imgtime = $data->{"images"}[0]->{"startdate"};
$imgtitle = $data->{"images"}[0]->{"copyright"};
$imglink = $data->{"images"}[0]->{"copyrightlink"};
//判断是否只获取图片信息
if ($_GET['info']==='true') {
echo "{title:".$imgtitle.",url:".$imgurl.",link:".$imglink.",time:".$imgtime."}";
}else{
//若不是则跳转url
header("Location: $imgurl");
}
保存文件,最后打开网页查看效果,如果有不同需求网址后面加“?参数代码=true(或者false)”,参考上面的参数表格,完整代码就是(以随机为例):https://xx.xx/index.php?rand=true,酱婶儿的就OK啦,把这个链接放在主题模板的背景图接口就大功告成!
© 版权声明
分享是一种美德,转载请保留原链接
THE END
- 最新
- 最热
只看作者