宝塔搭建 Bing 每日一图 API 项目
时间:2025-06-09 16:54:20 阅读:455
Bing 每日一图是微软提供的精美壁纸服务,每天更新一张高质量图片。本教程将指导你如何使用宝塔面板搭建一个简单的 Bing 每日一图 API,并将其应用到你的网站中。
项目地址:https://github.com/chengjunchao/bing-pic-api/
代码调用:
<style>
body{
background-image: url(https://open.saintic.com/api/bingPic/)!important;
}
.main .login{
background-color: rgba(255, 255, 255, 0.8)!important;
}
</style>关于API:
这api接口是自建的,可供大家使用。
自建API方法:
首先在网站根目录新建一个php文件,例如:bing.php、api.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://api.hhhe.cn/bing/api.php?rand=true
举例随机调用: (刷新后,图片随机显示,只是举例,链接可能会失效。)
https://api.hhhe.cn/bing/api.php?rand=true
还可以用PM2管理器安装API项目
进入宝塔商店安装PM2管理器
安装好进入设置
添加axios、 body-parser 、express 这几个模块

安装基于宝塔安装。
git clone https://github.com/chengjunchao/bing-pic-api.git npm install #安装依赖项 npm start #运行
PM2管理器安装项目

注意:
启动文件写app.js
访问地址:IP:2020/showtoday
如果不能打开注意在安全中开启2020端口
如果设置域名访问,可以点击后面的映射。
通过这个简单的教程,你已经成功搭建了一个 Bing 每日一图 API,并可以将其应用到你的网站中。这不仅能美化你的网站,还能为访客提供新鲜的高质量壁纸。你可以根据自己的需求进一步扩展这个 API 的功能。

网友评论