Amazon 已於 **2022 年 5 月 1 日** 正式關閉 Alexa Web Information Service (AWIS) 公開 API。原文中的 `data.alexa.com/data` 端點**已無法使用**,回傳 404 或空回應。本文保留原始碼供參考,**生產環境請改用替代方案**。
<?php
function getAlexaRank(string $domain): int
{
$url = "https://data.alexa.com/data/?cli=10&dat=snba&ver=7.0&url=" . urlencode($domain);
$xml = @file_get_contents($url);
if ($xml === false) return 0;
$parser = xml_parser_create();
xml_parse_into_struct($parser, $xml, $vals);
xml_parser_free($parser);
foreach ($vals as $item) {
if (isset($item['tag']) && strtoupper($item['tag']) === 'POPULARITY') {
return (int) ($item['attributes']['TEXT'] ?? 0);
}
}
return 0;
}
// 使用
echo getAlexaRank('google.com'); // 曾回傳 1
| 服務 | 資料來源 | 免費額度 | API 支援 | 適用場景 |
|---|---|---|---|---|
| SimilarWeb | 面板資料 + ISP 合作 | 有限免費查詢 | 付費 API | 市場分析、競品流量估算 |
| Semrush | 點擊流 + 第三方 | 10 次/天免費 | 付費 API | SEO 關鍵字、流量估算 |
| Ahrefs | 自有爬蟲 | 無免費 API | 付費 API | 反向連結、自然搜尋流量 |
| DataForSEO | 聚合多源 | 按量付費 | REST API | 大規模自動化 SEO 工具 |
| 自建指標 | Google Analytics / Search Console | 免費 (自有網站) | Reporting API | 自有站點真實流量 |
<?php
// 使用 Google Analytics Data API (GA4) 取得過去 28 天瀏覽量
require 'vendor/autoload.php';
use Google\Analytics\Data\V1beta\BetaAnalyticsDataClient;
$client = new BetaAnalyticsDataClient(['credentials' => 'path/to/service-account.json']);
$response = $client->runReport([
'property' => 'properties/' . GA4_PROPERTY_ID,
'dateRanges' => [['start_date' => '28daysAgo', 'end_date' => 'today']],
'metrics' => [['name' => 'screenPageViews']],
'dimensions' => [['name' => 'hostname']],
]);
foreach ($response->getRows() as $row) {
echo $row->getDimensionValues()[0]->getValue() . ': ' . $row->getMetricValues()[0]->getValue() . "\n";
}
Alexa 排名已成歷史名詞。**請勿在新專案中依賴 Alexa 排名**。改用: