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 相关文章推荐
PHP 代码规范小结
Mar 08 PHP
PHP 查找字符串常用函数介绍
Jun 07 PHP
php 地区分类排序算法
Jul 01 PHP
php switch语句多个值匹配同一代码块的实现
Mar 03 PHP
ThinkPHP缓存方法S()概述
Jun 13 PHP
php导入大量数据到mysql性能优化技巧
Dec 29 PHP
php使用cookie保存用户登录的用户名实例
Jan 26 PHP
php使用MySQL保存session会话的方法
Jun 18 PHP
PHP基于cookie与session统计网站访问量并输出显示的方法
Jan 15 PHP
php图片添加文字水印实现代码
Mar 15 PHP
PHP+Ajax无刷新带进度条图片上传示例
Feb 08 PHP
cakephp2.X多表联合查询join及使用分页查询的方法
Feb 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 URL编码解码函数代码
2009/03/10 PHP
PHP代码保护--Zend Guard的使用详解
2013/06/03 PHP
php函数与传递参数实例分析
2014/11/15 PHP
你不知道的文件上传漏洞php代码分析
2016/09/29 PHP
php实现的pdo公共类定义与用法示例
2017/07/19 PHP
Apache+PHP+MySQL搭建PHP开发环境图文教程
2020/08/06 PHP
利用webqq协议使用python登录qq发消息源码参考
2013/04/08 Javascript
js获取本机的外网/广域网ip地址完整源码
2013/08/12 Javascript
Js base64 加密解密介绍
2013/10/11 Javascript
js出生日期 年月日级联菜单示例代码
2014/01/10 Javascript
javascript实现汉字转拼音代码分享
2015/04/20 Javascript
原生js和jquery实现图片轮播淡入淡出效果
2015/04/23 Javascript
基于jQuery实现的扇形定时器附源码下载
2015/10/20 Javascript
js仿百度切换皮肤功能(html+css)
2016/07/10 Javascript
跟老齐学Python之集成开发环境(IDE)
2014/09/12 Python
Django自定义认证方式用法示例
2017/06/23 Python
Python DataFrame 设置输出不显示index(索引)值的方法
2018/06/07 Python
Python os.rename() 重命名目录和文件的示例
2018/10/25 Python
啥是佩奇?使用Python自动绘画小猪佩奇的代码实例
2019/02/20 Python
Python可以实现栈的结构吗
2020/05/27 Python
Python中lru_cache的使用和实现详解
2021/01/25 Python
HTML5 的新的表单元素(datalist/keygen/output)使用介绍
2013/07/19 HTML / CSS
StubHub意大利:购买和出售全球演唱会和体育赛事门票
2017/11/21 全球购物
亚洲领先的设计购物网站:Pinkoi
2020/11/26 全球购物
应届大专毕业生个人自荐信
2013/09/22 职场文书
计算机应用与科学个人的自我评价
2013/11/15 职场文书
中国梦读书活动总结
2014/07/10 职场文书
技术经济专业求职信
2014/09/03 职场文书
大一工商管理职业生涯规划:有梦最美,行动相随
2014/09/18 职场文书
材料员岗位职责
2015/02/10 职场文书
匿名信格式范文
2015/05/27 职场文书
2019职场实习报告该怎么写?
2019/07/01 职场文书
学习师德师风的心得体会(2篇)
2019/10/08 职场文书
Apache POI的基本使用详解
2021/11/07 Servers
DIV CSS实现网页背景半透明效果
2021/12/06 HTML / CSS
MySQL数据库实验之 触发器和存储过程
2022/06/21 MySQL