教程和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啦,把这个链接放在主题模板的背景图接口就大功告成!
免责声明:
此内容由本站网友原创或转载自网络公开渠道,仅供学习测试使用,禁止商用,著作权归原作者所有,本站不对此内容担负法律责,请于下载后24小时内删除,如发现内容侵权或违规,请联系本站,我们将在12小时内及时做删除或屏蔽处理!