Compete 類別封裝了對 Compete API 的呼叫,支援獨立訪客、造訪次數、排名、頁面瀏覽、平均停留、造訪/人次、頁面/造訪、關注度、每日觸達、性別、年齡、收入等指標。
class Compete {
const API_BASE_URL = 'https://apps.compete.com/sites/:domain/trended/:metric/?apikey=:key';
private $_apiKey;
public function __construct($apiKey) { $this->_apiKey = $apiKey; }
public function __call($name, $args) { /* 對應指標代碼 */ }
public function get($site, $metric) { /* curl 獲取 JSON 解析 */ }
private function _get($url) { /* curl 執行 */ }
}
$compete = new Compete('YOUR_API_KEY');
$uv = $compete->uniqueVisitors('example.com');
$rank = $compete->rank('example.com');
透過魔術方法 __call 將易讀方法名映射至 API 指標代碼,程式碼簡潔易維護。