1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
| <?php header('Content-Type: application/json'); header("Access-Control-Allow-Origin: *");
$apiConfig = [ 'baseUrl' => '[你的 Umami 部署地址,如 https://umami.haiskyblog.top]', 'token' => '[你的 Umami Token]', 'websiteId' => '[你的 Umami 网站 ID]' ]; $cacheConfig = [ 'file' => 'umami_cache.json', 'time' => 600 ]; $path = '/p/'; $site = '[你的网站地址,如 https://blog.everfu.cn]';
$currentTimestamp = time() * 1000;
$startTimestamps = [ 'today' => strtotime("today") * 1000, 'yesterday' => strtotime("yesterday") * 1000, 'lastMonth' => strtotime("-1 month") * 1000, 'lastYear' => strtotime("-1 year") * 1000 ];
function fetchUmamiData($config, $startAt, $endAt, $type = 'url', $limit = 500) { $url = "{$config['baseUrl']}/api/websites/{$config['websiteId']}/metrics?" . http_build_query([ 'startAt' => $startAt, 'endAt' => $endAt, 'type' => $type, 'limit' => $limit ]); $options = [ 'http' => [ 'method' => 'GET', 'header' => [ "Authorization: Bearer {$config['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 json_encode(["error" => "Error fetching data: " . $error['message'], "url" => $url]); return null; }
global $path; return array_values(array_filter(json_decode($response, true), fn($item) => strpos($item['x'], $path) === 0)); }
function isCacheValid($cacheConfig) { return file_exists($cacheConfig['file']) && (time() - filemtime($cacheConfig['file']) < $cacheConfig['time']); }
function readCache($cacheConfig) { return file_get_contents($cacheConfig['file']); }
function writeCache($cacheConfig, $data) { file_put_contents($cacheConfig['file'], json_encode($data)); }
function getProcessedData($config, $startTimestamps, $currentTimestamp) { $data = fetchUmamiData($config, $startTimestamps['today'], $currentTimestamp); if (!$data) return [];
$responseData = array_map(fn($item) => ["url" => $item['x'], "pv" => $item['y']], $data); usort($responseData, fn($a, $b) => $b['pv'] <=> $a['pv']); global $site;
$processedData = []; foreach (array_slice($responseData, 0, 5) as $item) { $url = $item['url']; $title = fetchPageTitle($site . $url); $processedData[] = ["url" => $url, "title" => $title]; }
return $processedData; }
function fetchPageTitle($url) { $html = @file_get_contents($url); if ($html === FALSE) { return "Unknown Title"; }
preg_match("/<h1 class=\"post-title\">(.*?)<\/h1>/is", $html, $matches); return $matches[1] ?? "Unknown Title"; }
if (isCacheValid($cacheConfig)) { echo readCache($cacheConfig); } else { $responseData = getProcessedData($apiConfig, $startTimestamps, $currentTimestamp); writeCache($cacheConfig, $responseData); echo json_encode($responseData); } ?>
|