feat(info.php): 添加通过umami API获取访问量数据的功能
info.php 文件现在通过开源项目 umami 的 API 获取访问量数据,并支持缓存以提高性能。该功能替代了原有的自定义数据获取逻辑,简化了维护并提高了数据的准确性。
This commit is contained in:
parent
2a2197d89d
commit
57d66e33b5
77
info.php
Normal file
77
info.php
Normal file
@ -0,0 +1,77 @@
|
||||
<?php
|
||||
header('Content-Type: application/json');
|
||||
header("Access-Control-Allow-Origin: *");
|
||||
|
||||
// 配置 Umami API 的凭据
|
||||
$apiBaseUrl = 'https://um.zhheo.com';
|
||||
$token = '你的tocken';
|
||||
$websiteId = '你的网站id';
|
||||
$cacheFile = 'umami_cache.json';
|
||||
$cacheTime = 600; // 缓存时间为10分钟(600秒)
|
||||
|
||||
// 获取当前时间戳(毫秒级)
|
||||
$currentTimestamp = time() * 1000;
|
||||
|
||||
// Umami API 的起始时间戳(毫秒级)
|
||||
$startTimestampToday = strtotime("today") * 1000;
|
||||
$startTimestampYesterday = strtotime("yesterday") * 1000;
|
||||
$startTimestampLastMonth = strtotime("-1 month") * 1000;
|
||||
$startTimestampLastYear = strtotime("-1 year") * 1000;
|
||||
|
||||
// 定义 Umami API 请求函数
|
||||
function fetchUmamiData($apiBaseUrl, $websiteId, $startAt, $endAt, $token) {
|
||||
$url = "$apiBaseUrl/api/websites/$websiteId/stats?" . http_build_query([
|
||||
'startAt' => $startAt,
|
||||
'endAt' => $endAt
|
||||
]);
|
||||
$options = [
|
||||
'http' => [
|
||||
'method' => 'GET',
|
||||
'header' => [
|
||||
"Authorization: Bearer $token",
|
||||
"Content-Type: application/json"
|
||||
]
|
||||
]
|
||||
];
|
||||
$context = stream_context_create($options);
|
||||
$response = @file_get_contents($url, false, $context);
|
||||
|
||||
if ($response === FALSE) {
|
||||
$error = error_get_last();
|
||||
echo "Error fetching data: " . $error['message'] . "\n";
|
||||
echo "URL: " . $url . "\n";
|
||||
return null;
|
||||
}
|
||||
|
||||
return json_decode($response, true);
|
||||
}
|
||||
|
||||
// 检查缓存文件是否存在且未过期
|
||||
if (file_exists($cacheFile) && (time() - filemtime($cacheFile) < $cacheTime)) {
|
||||
// 读取缓存文件
|
||||
$cachedData = file_get_contents($cacheFile);
|
||||
echo $cachedData;
|
||||
} else {
|
||||
// 获取统计数据
|
||||
$todayData = fetchUmamiData($apiBaseUrl, $websiteId, $startTimestampToday, $currentTimestamp, $token);
|
||||
$yesterdayData = fetchUmamiData($apiBaseUrl, $websiteId, $startTimestampYesterday, $startTimestampToday, $token);
|
||||
$lastMonthData = fetchUmamiData($apiBaseUrl, $websiteId, $startTimestampLastMonth, $currentTimestamp, $token);
|
||||
$lastYearData = fetchUmamiData($apiBaseUrl, $websiteId, $startTimestampLastYear, $currentTimestamp, $token);
|
||||
|
||||
// 组装返回的 JSON 数据
|
||||
$responseData = [
|
||||
"today_uv" => $todayData['visitors']['value'] ?? null,
|
||||
"today_pv" => $todayData['pageviews']['value'] ?? null,
|
||||
"yesterday_uv" => $yesterdayData['visitors']['value'] ?? null,
|
||||
"yesterday_pv" => $yesterdayData['pageviews']['value'] ?? null,
|
||||
"last_month_pv" => $lastMonthData['pageviews']['value'] ?? null,
|
||||
"last_year_pv" => $lastYearData['pageviews']['value'] ?? null
|
||||
];
|
||||
|
||||
// 将数据写入缓存文件
|
||||
file_put_contents($cacheFile, json_encode($responseData));
|
||||
|
||||
// 输出 JSON 数据
|
||||
echo json_encode($responseData);
|
||||
}
|
||||
?>
|
@ -32,9 +32,14 @@ project/
|
||||
├── index.html # 主页面
|
||||
├── styles.css # 样式文件
|
||||
├── script.js # 核心逻辑
|
||||
├── info.php # 访问量数据接口(需要自己写一个数据获取API)
|
||||
├── info.php # 访问量数据接口(通过开源项目umami的API获取数据)
|
||||
└── images/ # 图片资源目录
|
||||
```
|
||||
## 文件说明
|
||||
```
|
||||
info.php 文件来源开源仓库:https://github.com/zhheo/HeoPVBridge
|
||||
本项目部分灵感来源:https://immmmm.com/hi-umami-api/
|
||||
```
|
||||
|
||||
## 核心配置说明
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user