站点图标 咕嘟博客

PHP获取bing每日美图的API调用源码

本站的登录注册页面使用了Bing每日美图的api,调用Bing每日美图作为页面背景。

原主题调用的是主题作者的API,总感觉不太好。遂搜索了下,找到这份调用Bing每日美图API的PHP源码。

需要的同学,只要在网站目录下找一个地方新建一个php文件如bing.php,然后将下述代码粘贴进去,保存,设置好权限就可以使用了。

嫌麻烦的同学也可以调用本站的接口:

https://www.boilog.com/tools/bing/bing.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."api author:Otstar,api author link:https://www.ixk.me"."}";
}
else {
//若不是则跳转url
    header("Location: $imgurl");
}

文章参考来源:

退出移动版