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 翻页 实例代码
Aug 07 PHP
分享下PHP register_globals 值为on与off的理解
Sep 26 PHP
解决PhpMyAdmin中导入2M以上大文件限制的方法分享
Jun 06 PHP
ThinkPHP的I方法使用详解
Jun 18 PHP
PHP针对常规模板引擎中与CSS/JSON冲突的解决方法
Aug 19 PHP
php数组转成json格式的方法
Mar 09 PHP
注意!PHP 7中不要做的10件事
Sep 18 PHP
PHP7新增运算符用法实例分析
Sep 26 PHP
php简单统计中文个数的方法
Sep 30 PHP
PHP基于openssl实现的非对称加密操作示例
Jan 11 PHP
php-fpm重启导致的程序执行中断问题详解
Apr 29 PHP
php ActiveMQ的安装与使用方法图文教程
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 静态化实现代码
2009/03/20 PHP
php随机输出名人名言的代码
2012/10/07 PHP
php中rename函数用法分析
2014/11/15 PHP
php使用post数组的键值创建同名变量并赋值的方法
2015/04/03 PHP
Zend Framework入门教程之Zend_Session会话操作详解
2016/12/08 PHP
php 基础函数
2017/02/10 PHP
JavaScript TO HTML 转换
2006/06/26 Javascript
javascript 处理HTML元素必须避免使用的一种方法
2009/07/30 Javascript
Javascript 遮罩层和加载效果代码
2013/08/01 Javascript
两个select多选模式的选项相互移动(示例代码)
2014/01/11 Javascript
利用js正则表达式验证手机号,email地址,邮政编码
2014/01/23 Javascript
浅谈JavaScript字符串拼接
2015/06/25 Javascript
jQuery自定义滚动条完整实例
2016/01/08 Javascript
详解Vue自定义过滤器的实现
2017/01/10 Javascript
bootstrap中模态框、模态框的属性实例详解
2017/02/17 Javascript
JS字符串长度判断,超出进行自动截取的实例(支持中文)
2017/03/06 Javascript
Python里隐藏的“禅”
2014/06/16 Python
python用Pygal如何生成漂亮的SVG图像详解
2017/02/10 Python
Python实现学生成绩管理系统
2020/04/05 Python
opencv python 2D直方图的示例代码
2018/07/20 Python
Scrapy框架使用的基本知识
2018/10/21 Python
python 实现图片修复(可用于去水印)
2020/11/19 Python
日本网路线上商品代购服务:转送JAPAN
2016/08/05 全球购物
Microsoft新加坡官方网站:购买微软最新软件和技术产品
2016/10/28 全球购物
Origins加拿大官网:雅诗兰黛集团高端植物护肤品牌
2017/11/19 全球购物
eBay加拿大站:eBay.ca
2019/06/20 全球购物
日本最大化妆品和美容产品的综合口碑网站:cosme shopping
2019/08/28 全球购物
抽象方法、抽象类怎样声明
2014/10/25 面试题
放飞梦想演讲稿
2014/05/05 职场文书
应急管理工作总结2015
2015/05/04 职场文书
幼儿园推普周活动总结
2015/05/07 职场文书
宾馆卫生管理制度
2015/08/06 职场文书
诚信高考倡议书
2019/06/24 职场文书
大学生奶茶店创业计划书
2019/06/25 职场文书
启迪人心的励志语录:脾气永远不要大于本事
2020/01/02 职场文书
读《儒林外史》有感:少一些功利,多一些真诚
2020/01/19 职场文书