Compete(已停止服務)曾提供網站流量統計 API,支援獨立訪客、訪問次數、排名、頁面瀏覽、平均停留、訪問深度、關注度、人口屬性等指標。
<?php
class Compete {
const USER_AGENT = 'Compete API wrapper for PHP';
const API_BASE_URL = 'https://apps.compete.com/sites/:domain/trended/:metric/?apikey=:key';
private $_urlKeys = [':domain', ':metric', ':key'];
private $_apiKey;
private $_toSearch = ['https://', 'www.'];
private $_toReplace = ['', ''];
private $_availableMetrics = [
'uv', 'vis', 'rank', 'pv', 'avgstay', 'vpp', 'ppv', 'att', 'reachd', 'attd', 'gen', 'age', 'inc'
];
private $_metrics = [
'uniqueVisitors' => 'uv', 'visits' => 'vis', 'rank' => 'rank', 'pageViews' => 'pv',
'averageStay' => 'avgstay', 'visitsPerson' => 'vpp', 'pagesVisit' => 'ppv',
'attention' => 'att', 'dailyReach' => 'reachd', 'dailyAttention' => 'attd',
'gender' => 'gen', 'age' => 'age', 'income' => 'inc'
];
public function __construct($apiKey) { $this->_apiKey = $apiKey; }
public function __call($name, $args) {
if (isset($this->_metrics[$name]) && isset($args[0]))
return $this->get($args[0], $this->_metrics[$name]);
throw new CompeteException($name . ' method does not exist.');
}
public function get($site, $metric) {
if (!in_array($metric, $this->_availableMetrics))
throw new CompeteException($metric . ' - wrong metric.');
$url = str_replace($this->_urlKeys,
[$this->_prepareUrl($site), $metric, $this->_apiKey],
self::API_BASE_URL);
$data = json_decode($this->_get($url));
if (isset($data->status_message))
throw new CompeteException('Status: ' . $data->status . '. ' . $data->status_message);
return $data;
}
private function _prepareUrl($url) {
return str_replace($this->_toSearch, $this->_toReplace, $url);
}
private function _get($url) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_USERAGENT, self::USER_AGENT);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
return curl_exec($ch);
}
}
class CompeteException extends Exception {}
<?php
$apiKey = 'YOUR_COMPETE_API_KEY';
$compete = new Compete($apiKey);
try {
echo "Unique Visitors: "; var_dump($compete->uniqueVisitors('example.com'));
echo "Visits: "; var_dump($compete->visits('example.com'));
echo "Rank: "; var_dump($compete->rank('example.com'));
echo "Page Views: "; var_dump($compete->pageViews('example.com'));
// 更多指標...
} catch (CompeteException $e) {
echo 'Error: ' . $e->getMessage();
}
?>
| 方法名 | API 指標 | 說明 | 權限 |
|---|---|---|---|
| uniqueVisitors() | uv | 獨立訪客數 | Basic |
| visits() | vis | 訪問次數 | Basic |
| rank() | rank | 網站排名 | Basic |
| pageViews() | pv | 頁面瀏覽量 | All-Access |
| averageStay() | avgstay | 平均停留時間 | All-Access |
| visitsPerson() | vpp | 人均訪問次數 | All-Access |
| pagesVisit() | ppv | 每訪問頁數 | All-Access |
| attention() | att | 關注度 | All-Access |
| dailyReach() | reachd | 日覆蓋率 | All-Access |
| dailyAttention() | attd | 日關注度 | All-Access |
| gender() | gen | 性別分佈 | All-Access |
| age() | age | 年齡分佈 | All-Access |
| income() | inc | 收入分佈 | All-Access |
Compete 已於 2016 年 12 月停止服務,本類僅供歷史參考或遷移至 SimilarWeb、SEMrush、Ahrefs 等替代 API 時的架構參考。