必应Bing每日一图,API代码
在网站根目录新建一个php文件,例如:bing.php,然后复制下面的代码。
Api源码:
<?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"); }
可用参数:
参数 | 含义 | 值 |
rand | 是否随机显示最近8天内的图片 | true 、 false(默认false) |
day | 指定的最近图片 | -1,0,1,2,3,4,5,6,7 (0 今天,-1 昨天) |
size | 指定图片大小 | 分辨率(默认1920x1080,其他分辨率可根据下面的提示填写) |
info | 获取图片基础信息(json格式) | true 、 false(默认false) |
分辨率:
1920x1080
1366x768
1280x768
1024x768
800x600
800x480
768x1280
720x1280
640x480
480x800
400x240
320x240
240x320
注:以上所有参数非必填,默认参数为: ?rand=false&day=0&size=1920×1080&info=false
版权声明:本文由 LzxBlog 发布,如需转载请注明出处。