随机PHP必应每日一图APi代码及调用方法

曹桑烈 5月前 235

教程和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啦,把这个链接放在主题模板的背景图接口就大功告成!

看过的人 (5)
  • admin
  • Mr.Chen
  • 691
  • as
  • 曹桑烈
最新回复 (12)
返回