自用 PHP Bing每日壁纸 API 源码分享(支持随机/多尺寸/多地区)
今天给大家分享一个我自己在用的 Bing 每日壁纸 API 源码。代码非常轻量,不需要数据库,上传到网站目录即可使用。支持随机返回近8天壁纸、多尺寸、多地区,自带调用计数,完全免费无限制。
📈 接口实时调用统计(动态图表)
以下数据直接读取服务器的 data/bing_stat.txt,并渲染成直观的图表(每次刷新页面数据都会变化):
🖼️ 接口图片实时预览墙
下方图片通过接口实时请求(已锁定为一行两张,固定高度裁剪,不会严重下滑):
🛠️ 接口参数说明
| 参数 | 说明 |
|---|---|
?rand=true | 随机返回近8天壁纸 |
?day=1 | 日期偏移:0=今日,1=昨日,7=7天前 |
?mkt=en-US | 地区:zh-CN / en-US / ja-JP |
?size=1080x1920 | 尺寸:1920x1080 / 1366x768 / 1080x1920 |
?format=json | 返回JSON格式(包含标题、链接等详细信息) |
💻 调用示例
1. 今日壁纸:
https://yuxli.cn/api/bing.php
2. 随机壁纸(每次刷新不同):
https://yuxli.cn/api/bing.php?rand=true
3. 手机竖屏壁纸:
https://yuxli.cn/api/bing.php?size=1080x1920
4. 英文区壁纸:
https://yuxli.cn/api/bing.php?mkt=en-US
5. 昨日壁纸:
https://yuxli.cn/api/bing.php?day=1
6. 返回JSON数据(获取标题、链接等):
https://yuxli.cn/api/bing.php?format=json
💻 完整源码
请将以下代码保存为 bing.php,上传至你网站根目录下的 api 文件夹中,并确保该文件夹有 PHP 的写入权限。统计文件会自动存放到 api/data/ 目录。
<?php/** * Bing每日壁纸API * 文件名:bing.php */// 统计文件(统一放到 /api/data/ 目录)$dataDir = __DIR__ . '/data';if (!is_dir($dataDir)) { mkdir($dataDir, 0755, true);}$countFile = $dataDir . '/bing_stat.txt';if (!file_exists($countFile)) { file_put_contents($countFile, '0');}$total = (int)file_get_contents($countFile);$total++;file_put_contents($countFile, $total);// 参数$rand = isset($_GET['rand']) && $_GET['rand'] === 'true';$day = isset($_GET['day']) ? intval($_GET['day']) : 0;$mkt = isset($_GET['mkt']) ? trim($_GET['mkt']) : 'zh-CN';$size = isset($_GET['size']) ? trim($_GET['size']) : '1920x1080';$format = isset($_GET['format']) ? trim($_GET['format']) : 'img';// 校验尺寸$allowedSizes = ['1920x1080', '1366x768', '1080x1920'];if (!in_array($size, $allowedSizes)) { $size = '1920x1080';}// 校验day范围(0-7)$day = max(0, min(7, $day));// 从Bing获取壁纸数据$bingApi = "https://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=8&mkt=" . $mkt;$ch = curl_init($bingApi);curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);curl_setopt($ch, CURLOPT_TIMEOUT, 5);curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36');$response = curl_exec($ch);curl_close($ch);if (!$response) { header("Location: https://picsum.photos/$size", true, 302); exit;}$data = json_decode($response, true);if (empty($data['images'])) { header("Location: https://picsum.photos/$size", true, 302); exit;}$images = $data['images'];// 选择哪一天的壁纸if ($rand) { $index = array_rand($images);} else { $index = $day;}if (!isset($images[$index])) { $index = 0;}$image = $images[$index];// 拼接完整图片URL$imgUrl = 'https://www.bing.com' . $image['url'];// 替换尺寸$imgUrl = preg_replace('/\d{4}x\d{4}/', $size, $imgUrl);// 如果是json格式,返回JSONif ($format === 'json') { header("Access-Control-Allow-Origin: *"); header('Content-Type: application/json; charset=utf-8'); echo json_encode([ 'code' => 200, 'url' => $imgUrl, 'title' => $image['copyright'], 'link' => 'https://www.bing.com' . $image['copyrightlink'], 'size' => $size, 'total_call' => $total ], JSON_UNESCAPED_UNICODE); exit;}// 默认直接跳转图片header("Location: " . $imgUrl, true, 302);exit;—— 免费无限制使用,欢迎大家测试 ——