创建api.php,放在根目录
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}"); // 跳转至目标图像
?>
以下更全代码
<?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://网址/api.php?rand=true,酱婶儿的就OK啦,把这个链接放在主题模板的背景图接口就大功告成!