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新手上路(十四)
Oct 09 PHP
php中变量及部分适用方法
Mar 27 PHP
php开发过程中关于继承的使用方法分享
Jun 17 PHP
js和php邮箱地址验证的实现方法
Jan 09 PHP
PHP实现设计模式中的抽象工厂模式详解
Oct 11 PHP
在Nginx上部署ThinkPHP项目教程
Feb 02 PHP
round robin权重轮循算法php实现代码
May 28 PHP
PHP获取用户客户端真实IP的解决方案
Oct 10 PHP
php微信公众号开发模式详解
Nov 28 PHP
PHP实现简单的模板引擎功能示例
Sep 02 PHP
在云虚拟主机部署thinkphp5项目的步骤详解
Dec 21 PHP
laravel 框架结合关联查询 when()用法分析
Nov 22 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
雄兵连:天使彦天使彦为爱折翼,彦和炙心同时念动的誓言!
2020/03/02 国漫
PHP取余函数介绍MOD(x,y)与x%y
2014/05/15 PHP
PHP实现双链表删除与插入节点的方法示例
2017/11/11 PHP
如何做到打开一个页面,过几分钟自动转到另一页面
2007/04/20 Javascript
JObj预览一个JS的框架
2008/03/13 Javascript
借用Google的Javascript API Loader来加速你的网站
2009/01/28 Javascript
jQuery 使用手册(五)
2009/09/23 Javascript
js创建对象的区别示例介绍
2014/07/24 Javascript
CSS3,HTML5和jQuery搜索框集锦
2014/12/02 Javascript
jquery使用经验小结
2015/05/20 Javascript
JavaScript中Math.SQRT2属性的使用详解
2015/06/14 Javascript
使用nodejs爬取前程无忧前端技能排行
2017/05/06 NodeJs
浅谈react.js中实现tab吸顶效果的问题
2017/09/06 Javascript
微信小程序模板template简单用法示例
2018/12/04 Javascript
在Vant的基础上实现添加表单验证框架的方法示例
2018/12/05 Javascript
Layui实现带查询条件的分页
2019/07/27 Javascript
[55:11]完美世界DOTA2联赛PWL S2 SZ vs LBZS 第一场 11.26
2020/11/30 DOTA
[46:42]DOTA2-DPC中国联赛正赛 Aster vs Magma BO3 第二场 3月5日
2021/03/11 DOTA
Python中的特殊语法:filter、map、reduce、lambda介绍
2015/04/14 Python
Django框架封装外部函数示例
2019/05/28 Python
python 函数中的参数类型
2020/02/11 Python
详解Python设计模式之策略模式
2020/06/15 Python
聊聊python中的异常嵌套
2020/09/01 Python
python爬虫实现爬取同一个网站的多页数据的实例讲解
2021/01/18 Python
html5 标签
2009/07/16 HTML / CSS
美国学校用品、教室和教学商店:Discount School Supply
2018/04/04 全球购物
物业管理求职自荐信
2013/09/25 职场文书
汽车运用工程专业毕业生推荐信
2013/12/25 职场文书
志愿者活动总结报告
2014/06/27 职场文书
护士2015年终工作总结
2015/04/29 职场文书
525心理健康活动总结
2015/05/08 职场文书
军训后的感想
2015/08/07 职场文书
2016年党员读书月活动总结
2016/04/06 职场文书
导游词之江苏溱潼古镇
2019/11/27 职场文书
Linux系统下安装PHP7.3版本
2021/06/26 PHP
MySQL 表锁定 LOCK和UNLOCK TABLES的 SQL语法
2022/04/18 MySQL