From 57d66e33b5ac5f0ebc56106bcc6f18abbfeecfee Mon Sep 17 00:00:00 2001 From: Snowz <372492339@qq.com> Date: Fri, 2 May 2025 22:53:57 +0800 Subject: [PATCH] =?UTF-8?q?feat(info.php):=20=E6=B7=BB=E5=8A=A0=E9=80=9A?= =?UTF-8?q?=E8=BF=87umami=20API=E8=8E=B7=E5=8F=96=E8=AE=BF=E9=97=AE?= =?UTF-8?q?=E9=87=8F=E6=95=B0=E6=8D=AE=E7=9A=84=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit info.php 文件现在通过开源项目 umami 的 API 获取访问量数据,并支持缓存以提高性能。该功能替代了原有的自定义数据获取逻辑,简化了维护并提高了数据的准确性。 --- info.php | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ readme.md | 7 ++++- 2 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 info.php diff --git a/info.php b/info.php new file mode 100644 index 0000000..fa6482f --- /dev/null +++ b/info.php @@ -0,0 +1,77 @@ + $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); +} +?> diff --git a/readme.md b/readme.md index cdbd711..7ca9260 100644 --- a/readme.md +++ b/readme.md @@ -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/ +``` ## 核心配置说明