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

PHP 使用 Compete API 獲取網站流量統計:完整封裝類與用法

     編輯:PHP 綜合知識

Compete API 簡介

Compete(已停止服務)曾提供網站流量統計 API,支援獨立訪客、訪問次數、排名、頁面瀏覽、平均停留、訪問深度、關注度、人口屬性等指標。

PHP 封裝類

<?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 時的架構參考。

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