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 记录进行累加并显示总时长为秒的结果
Nov 04 PHP
简单实现限定phpmyadmin访问ip的方法
Mar 05 PHP
迅速确定php多维数组的深度的方法
Jan 07 PHP
destoon安装出现Internal Server Error的解决方法
Jun 21 PHP
Laravel框架学习笔记(二)项目实战之模型(Models)
Oct 15 PHP
php实现中文字符截取防乱码方法汇总
Apr 29 PHP
PHP实现事件机制的方法
Jul 10 PHP
php+ajax 实现输入读取数据库显示匹配信息
Oct 08 PHP
WordPress中用于获取文章信息以及分类链接的函数用法
Dec 18 PHP
PHP符合PSR编程规范的实例分享
Dec 21 PHP
PHP获取中国时间(上海时区时间)及美国时间的方法
Feb 23 PHP
PHP+MySQL高并发加锁事务处理问题解决方法
Apr 30 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 学习资料零碎东西
2010/12/04 PHP
PHP转换IP地址到真实地址的方法详解
2013/06/09 PHP
使用PHP实现生成HTML静态页面
2015/11/18 PHP
详解PHP的Yii框架中组件行为的属性注入和方法注入
2016/03/18 PHP
javascript按位非运算符的使用方法
2013/11/14 Javascript
JavaScript如何自定义trim方法
2015/07/28 Javascript
浅谈Jquery中Ajax异步请求中的async参数的作用
2016/06/06 Javascript
Jquery遍历select option和添加移除option的实现方法
2016/08/26 Javascript
jQuery Validate设置onkeyup验证的实例代码
2016/12/09 Javascript
自学实现angularjs依赖注入
2016/12/20 Javascript
使用BootStrap进行轮播图的制作
2017/01/06 Javascript
使用vue与jquery实时监听用户输入状态的操作代码
2017/09/19 jQuery
Jquery实现获取子元素的方法分析
2019/08/24 jQuery
vue按需加载实例详解
2019/09/06 Javascript
layui使用templet格式化表格数据的方法
2019/09/16 Javascript
[02:37]2015国际邀请赛选手档案—LGD.Xiao8
2015/07/28 DOTA
编写Python爬虫抓取暴走漫画上gif图片的实例分享
2016/04/20 Python
python实现识别手写数字 python图像识别算法
2020/03/23 Python
Php多进程实现代码
2018/05/07 Python
Python matplotlib以日期为x轴作图代码实例
2019/11/22 Python
新手学python应该下哪个版本
2020/06/11 Python
基于tensorflow for循环 while循环案例
2020/06/30 Python
10个示例带你掌握python中的元组
2020/11/23 Python
日本最佳原创设计品牌:Felissimo(芬理希梦)
2019/03/19 全球购物
有原因的手表:Flex Watches
2019/03/23 全球购物
法律六进活动方案
2014/03/13 职场文书
合作意向书格式及范文
2014/03/31 职场文书
报到证办理个人委托书
2014/10/06 职场文书
销售2014年度工作总结
2014/12/08 职场文书
求职自我评价怎么写
2015/03/09 职场文书
护士自荐信范文
2015/03/25 职场文书
公司借款担保书
2015/09/22 职场文书
2016年植树节红领巾广播稿
2015/12/17 职场文书
高中政治教学反思
2016/02/23 职场文书
2019年浪漫婚礼证婚词
2019/06/27 职场文书
Spring Boot 实现敏感词及特殊字符过滤处理
2021/06/29 Java/Android