首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 相關知識

PHP 串接 Compete API 取得網站流量統計資料的類別封裝

     編輯:PHP 相關知識

PHP 串接 Compete API 取得網站流量統計資料的類別封裝

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 指標代碼,程式碼簡潔易維護。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved