php获得网站访问统计信息类Compete API用法实例


Posted in PHP onApril 02, 2015

本文实例讲述了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程序设计有所帮助。

PHP 相关文章推荐
利用 window_onload 实现select默认选择
Oct 09 PHP
关于mysql 字段的那个点为是定界符
Jan 15 PHP
经典的PHPer为什么被认为是草根?
Apr 02 PHP
PHP STRING 陷阱原理说明
Jul 24 PHP
php开发过程中关于继承的使用方法分享
Jun 17 PHP
解析百度搜索结果link?url=参数分析 (全)
Oct 09 PHP
在wamp集成环境下升级php版本(实现方法)
Jul 01 PHP
php生成缩略图填充白边(等比缩略图方案)
Dec 25 PHP
php出现web系统多域名登录失败的解决方法
Sep 30 PHP
推荐一款PHP+jQuery制作的列表分页的功能模块
Oct 14 PHP
PHP判断IP并转跳到相应城市分站的方法
Mar 25 PHP
php版微信开发之接收消息,自动判断及回复相应消息的方法
Sep 23 PHP
php实现从上传文件创建缩略图的方法
Apr 02 #PHP
php调用KyotoTycoon简单实例
Apr 02 #PHP
PHP中数据类型转换的三种方式
Apr 02 #PHP
php在apache环境下实现gzip配置方法
Apr 02 #PHP
PHP中使用socket方式GET、POST数据实例
Apr 02 #PHP
php获取百度收录、百度热词及百度快照的方法
Apr 02 #PHP
php中实现获取随机数组列表的自定义函数
Apr 02 #PHP
You might like
php empty() 检查一个变量是否为空
2011/11/10 PHP
使用PHP+AJAX让WordPress动态加载文章的教程
2015/12/11 PHP
高质量PHP代码的50个实用技巧必备(上)
2016/01/22 PHP
PHP-FPM实现性能优化
2016/03/31 PHP
如何在centos8自定义目录安装php7.3
2019/11/28 PHP
AJAX的跨域与JSONP(为文章自动添加短址的功能)
2010/01/17 Javascript
Jquery仿IGoogle实现可拖动窗口示例代码
2014/08/22 Javascript
JS实现点击按钮获取页面高度的方法
2015/11/02 Javascript
JS显示日历和天气的方法
2016/03/01 Javascript
jQuery实现内容定时切换效果完整实例
2016/04/06 Javascript
浅谈javascript中关于日期和时间的基础知识
2016/07/13 Javascript
关于vue.js弹窗组件的知识点总结
2016/09/11 Javascript
JS实现的图片预览插件与用法示例【不上传图片】
2016/11/25 Javascript
微信小程序之购物车功能
2020/09/23 Javascript
JavaScript数组去重算法实例小结
2018/05/07 Javascript
JavaScript折半查找(二分查找)算法原理与实现方法示例
2018/08/06 Javascript
利用d3.js实现蜂巢图表带动画效果
2019/09/03 Javascript
vue 更改连接后台的api示例
2019/11/11 Javascript
40行代码把Vue3的响应式集成进React做状态管理
2020/05/20 Javascript
解决Vue中使用keepAlive不缓存问题
2020/08/04 Javascript
[04:02]2014DOTA2国际邀请赛 BBC每日综述中国战队将再度登顶
2014/07/21 DOTA
[07:39]第一届亚洲邀请赛回顾视频
2017/02/14 DOTA
Python set集合类型操作总结
2014/11/07 Python
Python中一些自然语言工具的使用的入门教程
2015/04/13 Python
Python中使用items()方法返回字典元素对的教程
2015/05/21 Python
python协程用法实例分析
2015/06/04 Python
Python第三方Window模块文件的几种安装方法
2018/11/22 Python
Python面向对象思想与应用入门教程【类与对象】
2019/04/12 Python
在Python中过滤Windows文件名中的非法字符方法
2019/06/10 Python
python 梯度法求解函数极值的实例
2019/07/10 Python
python初步实现word2vec操作
2020/06/09 Python
Pycharm2020.1安装中文语言插件的详细教程(不需要汉化)
2020/08/07 Python
英国儿童设计师服装的领先零售商:Base
2019/03/17 全球购物
日本最大美瞳直送网:Morecontact(中文)
2019/04/03 全球购物
Linux如何压缩可执行文件
2013/10/21 面试题
食品安全承诺书范文
2014/08/29 职场文书