php获得网站访问统计信息类Compete API用法实例
程序员文章站
2022-04-06 12:40:22
本文实例讲述了php获得网站访问统计信息类compete api用法。分享给大家供大家参考。具体如下:
这里使用php获得网站访问统计信息类compete api,com...
本文实例讲述了php获得网站访问统计信息类compete api用法。分享给大家供大家参考。具体如下:
这里使用php获得网站访问统计信息类compete api,compete是一个专门用来统计网站信息的网站
<?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); } }
希望本文所述对大家的php程序设计有所帮助。
上一篇: Json.net日期格式化设置
下一篇: php调用KyotoTycoon简单实例