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 相关文章推荐
把从SQL中取出的数据转化成XMl格式
Oct 09 PHP
简单PHP上传图片、删除图片实现代码
May 12 PHP
PHP编码转换函数 自动转换字符集支持数组转换
Dec 16 PHP
php变量作用域的深入解析
Jun 03 PHP
浅析php过滤html字符串,防止SQL注入的方法
Jul 02 PHP
异步加载技术实现当滚动条到最底部的瀑布流效果
Sep 16 PHP
迁移PHP版本到PHP7
Feb 06 PHP
php实现的日历程序
Jun 18 PHP
php解析mht文件转换成html的实例
Mar 13 PHP
php 查找数组元素提高效率的方法详解
May 05 PHP
PHP命名空间定义与用法实例分析
Aug 14 PHP
ThinkPHP 5.x远程命令执行漏洞复现
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
phpMyAdmin下载、安装和使用入门教程
2007/05/31 PHP
10个超级有用的PHP代码片段果断收藏
2015/09/23 PHP
PHP设计模式之工厂模式详解
2017/10/24 PHP
javascript对数组的常用操作代码 数组方法总汇
2011/01/27 Javascript
Extjs407 getValue()和getRawValue()区别介绍
2013/05/21 Javascript
document.execCommand()的用法小结
2014/01/08 Javascript
JavaScript中Cookies的相关使用教程
2015/06/04 Javascript
gameboy网页闯关游戏(riddle webgame)--仿微信聊天的前端页面设计和难点
2016/02/21 Javascript
JS onkeypress兼容性写法详解
2016/04/27 Javascript
Vue.js学习之过滤器详解
2017/01/22 Javascript
一道面试题引发的对javascript类型转换的思考
2017/03/06 Javascript
react中使用swiper的具体方法
2018/05/15 Javascript
js图数据结构处理 迪杰斯特拉算法代码实例
2019/09/11 Javascript
微信小程序vant弹窗组件的实现方式
2020/02/21 Javascript
原生JS利用transform实现banner的无限滚动示例代码
2020/06/15 Javascript
Vue两种组件类型:递归组件和动态组件的用法
2020/08/06 Javascript
Javascript var变量删除原理及实现
2020/08/26 Javascript
[51:07]VGJ.S vs Pain 2018国际邀请赛小组赛BO2 第一场 8.17
2018/08/20 DOTA
python实现带验证码网站的自动登陆实现代码
2015/01/12 Python
python xlsxwriter创建excel图表的方法
2018/06/11 Python
使用python实现快速搭建简易的FTP服务器
2018/09/12 Python
利用Python实现手机短信监控通知的方法
2019/07/22 Python
利用python读取YUV文件 转RGB 8bit/10bit通用
2019/12/09 Python
土耳其国际性时尚购物网站:Modanisa
2018/01/19 全球购物
瑞典在互联网上最大的宠物商店:Animail
2020/10/31 全球购物
静态成员和非静态成员的区别
2012/05/12 面试题
生产厂长岗位职责
2014/02/21 职场文书
写给老婆的检讨书
2014/02/21 职场文书
走进敬老院活动总结
2014/07/10 职场文书
2015元旦晚会主持词(开场白+结束语)
2014/12/14 职场文书
会计稽核岗位职责
2015/04/13 职场文书
导游词之介休绵山
2019/12/31 职场文书
Python Matplotlib绘制条形图的全过程
2021/10/24 Python
vue封装数字翻牌器
2022/04/20 Vue.js
关于的python五子棋的算法
2022/05/02 Python
Win10玩csgo闪退如何解决?Win10玩csgo闪退的解决方法
2022/07/23 数码科技