此API只支持HTTPs調用,所有參數均僅適用於以 GET 方式進行請求,可直接插入img標籤中!
調用參數
參數代碼 | 參數含義 | 可用參數 |
rand | 是否隨機顯示最近8天內的圖片 | true or false |
day | 顯示指定的最近圖片 | -1,0,1,2,3,4,5,6,7(0為今天,-1為昨天) |
size | 指定獲取圖片大小 | 詳見下方可用解析度 |
info | 取得圖片基礎資訊(json格式) | true or false |
以上所有參數均非必要,默認參數為rand=false,day=0,size=1920×1080,info=false
可用解析度
1920×1080,1366×768,1280×768,1024×768,800×600,800×480,768 ×1280,720×1280,640×480,480×800,400×240,320×240,240×320
調用範例
默認調用
https://xx.xx/api/bing
不帶任何參數調用,顯示Bing當天圖片。
Info調用
https://xx.xx/api/bing?info=true
返回值:
{title:廬山冬季美景,中國江西省 ( © silkwayrain/Getty Images),url:https://www.bing.com/th?id=OHR. LiDong2022_ZH-CN9929478283_1920x1080.jpg,link:https://www.bing.com/search?q=%E7%AB%8B%E5%86%AC&form=hpcapt&mkt=zh-cn,time:20221106}
教程和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啦,把這個連結放在主題範本的背景圖介面就大功告成!
如想使用自建圖片庫則可查可此兩篇文章:
文章標題:PHP隨機Bing每日一圖APi代碼及調用方法
本文鏈接:https://angelal.cc/1229.html
文章版權:除非特別註明,否則均為AngelaL的原創文章,轉載必須以鏈接形式標明本文鏈接
本文最後更新發佈於:2025年02月26日 21:26, 某些文章具有時效性,若有錯誤或已失效,請在下方留言。