php獲得網(wǎng)站訪問(wèn)統(tǒng)計(jì)信息類(lèi)Compete API用法實(shí)例
本文實(shí)例講述了php獲得網(wǎng)站訪問(wèn)統(tǒng)計(jì)信息類(lèi)Compete API用法。分享給大家供大家參考。具體如下:
這里使用php獲得網(wǎng)站訪問(wèn)統(tǒng)計(jì)信息類(lèi)Compete API,Compete是一個(gè)專(zhuān)門(mén)用來(lái)統(tǒng)計(jì)網(wǎng)站信息的網(wǎng)站
<?php
// Check for dependencies
if (!function_exists('curl_init'))
throw new Exception('Compete needs the CURL PHP extension.');
if (!function_exists('json_decode'))
throw new Exception('Compete needs the JSON PHP extension.');
/**
* Base Compete exception class.
*/
class CompeteException extends Exception {}
/**
* Represents Compete API.
* @author Egor Gumenyuk (boo1ean0807 at gmail dot com)
* @package Compete
* @license Apache 2.0
*/
class Compete
{
/**
* Default usr agent.
*/
const USER_AGENT = 'Compete API wrapper for PHP';
/**
* Base url for api calls.
*/
const API_BASE_URL = 'http://apps.compete.com/sites/:domain/trended/:metric/?apikey=:key';
/**
* Masks for url params.
*/
private $_urlKeys = array(':domain', ':metric', ':key');
private $_apiKey;
/**
* For url cleaning.
*/
private $_toSearch = array('http://', 'www.');
private $_toReplace = array('', '');
/**
* List of available metrics.
*/
private $_availableMetrics = array(
// Description Auth type
'uv', // Unique Visitors Basic
'vis', // Visits Basic
'rank', // Rank Basic
'pv', // Page Views All-Access
'avgstay',// Average Stay All-Access
'vpp', // Visits/Person All-Access
'ppv', // Pages/Visit All-Access
'att', // Attention All-Access
'reachd', // Daily Reach All-Access
'attd', // Daily Attention All-Access
'gen', // Gender All-Access
'age', // Age All-Access
'inc', // Income All-Access
);
/**
* List of available methods for __call() implementation.
*/
private $_metrics = array(
'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'
);
/**
* Create access to Compete API.
* @param string $apiKey user's api key.
*/
public function __construct($apiKey) {
$this->_apiKey = $apiKey;
}
/**
* Implement specific methods.
*/
public function __call($name, $args) {
if (array_key_exists($name, $this->_metrics) && isset($args[0]))
return $this->get($args[0], $this->_metrics[$name]);
throw new CompeteException($name . ' method does not exist.');
}
/**
* Get data from Compete.
* @param string $site some domain.
* @param string $metric metric to get.
* @return stdClass Compete data.
* @throws CompeteException
*/
public function get($site, $metric) {
if (!in_array($metric, $this->_availableMetrics))
throw new CompeteException($metric . ' - wrong metric.');
$values = array(
$this->_prepareUrl($site),
$metric,
$this->_apiKey
);
// Prepare call url
$url = str_replace($this->_urlKeys, $values, self::API_BASE_URL);
// Retrieve data using HTTP GET method.
$data = json_decode($this->_get($url));
// Because of unsuccessful responses contain "status_message".
if (!isset($data->status_message))
return $data;
throw new CompeteException('Status: ' . $data->status . '. ' .$data->status_message);
}
/**
* Cut unnecessary parts of url.
* @param string $url some url.
* @return string trimmed url.
*/
private function _prepareUrl($url) {
return str_replace($this->_toSearch, $this->_toReplace, $url);
}
/**
* Execute http get method.
* @param string $url request url.
* @return string response.
*/
private function _get($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, self::USER_AGENT);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
return curl_exec($ch);
}
}
希望本文所述對(duì)大家的php程序設(shè)計(jì)有所幫助。
- PHP記錄搜索引擎蜘蛛訪問(wèn)網(wǎng)站足跡的方法
- php中記錄用戶(hù)訪問(wèn)過(guò)的產(chǎn)品,在cookie記錄產(chǎn)品id,id取得產(chǎn)品信息
- php下用cookie統(tǒng)計(jì)用戶(hù)訪問(wèn)網(wǎng)頁(yè)次數(shù)的代碼
- php利用cookie實(shí)現(xiàn)訪問(wèn)次數(shù)統(tǒng)計(jì)代碼
- php使用文本統(tǒng)計(jì)訪問(wèn)量的方法
- 使用PHP實(shí)現(xiàn)蜘蛛訪問(wèn)日志統(tǒng)計(jì)
- PHP基于cookie與session統(tǒng)計(jì)網(wǎng)站訪問(wèn)量并輸出顯示的方法
- php網(wǎng)站判斷用戶(hù)是否是手機(jī)訪問(wèn)的方法
- php使用cookie顯示用戶(hù)上次訪問(wèn)網(wǎng)站日期的方法
- PHP實(shí)現(xiàn)網(wǎng)站訪問(wèn)量計(jì)數(shù)器
- PHP簡(jiǎn)單實(shí)現(xiàn)記錄網(wǎng)站訪問(wèn)量功能示例
相關(guān)文章
刪除無(wú)限分類(lèi)并同時(shí)刪除它下面的所有子分類(lèi)的方法
今天晚上上Q的時(shí)候 ,一個(gè)以前的學(xué)員在網(wǎng)上問(wèn)我,怎么刪除一個(gè)無(wú)限分類(lèi)的時(shí)候同時(shí)刪除它下面的所有子分類(lèi)。我把代碼稍微整理了一下,發(fā)出來(lái),放在php學(xué)員問(wèn)答里面,方便以后的人查看 。2010-08-08
Windows下安裝PHP單元測(cè)試環(huán)境PHPUnit圖文教程
這篇文章主要介紹了Windows下安裝PHP單元測(cè)試環(huán)境PHPUnit圖文教程,按照本文操作即可在Windows下安裝成功PHPUnit,需要的朋友可以參考下2014-10-10
PHP四種統(tǒng)計(jì)在線人數(shù)方式詳細(xì)介紹
這篇文章主要介紹了用PHP來(lái)統(tǒng)計(jì)在線人數(shù)的四個(gè)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2022-09-09
php如何比較兩個(gè)浮點(diǎn)數(shù)是否相等詳解
這篇文章主要給大家介紹了關(guān)于php如何比較兩個(gè)浮點(diǎn)數(shù)是否相等的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-02-02
PHP中strcmp()和strcasecmp()函數(shù)字符串比較用法分析
這篇文章主要介紹了PHP中strcmp()和strcasecmp()函數(shù)字符串比較用法,結(jié)合實(shí)例形式較為詳細(xì)的分析了strcmp()和strcasecmp()函數(shù)字的功能,使用方法與區(qū)別,需要的朋友可以參考下2016-01-01
php帶密碼功能并下載遠(yuǎn)程文件保存本地指定目錄 修改加強(qiáng)版
php帶密碼功能并將遠(yuǎn)程文件下載到本地指定目錄修改版,需要的朋友可以參考下。2010-05-05
解析PHP中empty is_null和isset的測(cè)試
本篇文章是對(duì)PHP中empty is_null和isse的測(cè)試進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06

