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 相关文章推荐
杏林同学录(四)
Oct 09 PHP
PHP批量生成缩略图的代码
Jul 19 PHP
如何获知PHP程序占用多少内存(memory_get_usage)
Sep 23 PHP
基于PHP字符串的比较函数strcmp()与strcasecmp()的使用详解
May 15 PHP
php源代码安装常见错误与解决办法分享
May 28 PHP
PHP整数取余返回负数的相关解决方法
May 15 PHP
五款PHP代码重构工具推荐
Oct 14 PHP
PHP获取音频文件的相关信息
Jun 22 PHP
/etc/php-fpm.d/www.conf 配置注意事项
Feb 04 PHP
php 常用的系统函数
Feb 07 PHP
PHP面向对象之领域模型+数据映射器实例(分析)
Jun 21 PHP
php实现微信公众号创建自定义菜单功能的实例代码
Jun 11 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获取随机数字和字母的方法详解
2013/06/06 PHP
php简单实现多字节字符串翻转的方法
2015/03/31 PHP
laravel 使用auth编写登录的方法
2019/09/30 PHP
javascript Zifa FormValid 0.1表单验证 代码打包下载
2007/06/08 Javascript
Jquery 学习笔记(一)
2009/10/13 Javascript
uploadify多文件上传参数设置技巧
2015/11/16 Javascript
从0开始学Vue
2016/10/27 Javascript
vue实现仿淘宝结账页面实例代码
2017/11/08 Javascript
微信小程序获取手机系统信息的方法【附源码下载】
2017/12/07 Javascript
关于Webpack dev server热加载失败的解决方法
2018/02/22 Javascript
详解Vue结合后台的列表增删改案例
2018/08/21 Javascript
Vue组件间通信方法总结(父子组件、兄弟组件及祖先后代组件间)
2019/04/17 Javascript
详解vue+axios给开发环境和生产环境配置不同的接口地址
2019/08/16 Javascript
vue props 一次传多个值实例
2020/07/22 Javascript
[43:03]LGD vs Newbee 2019国际邀请赛小组赛 BO2 第一场 8.16
2019/08/19 DOTA
[52:12]FNATIC vs Infamous 2019国际邀请赛小组赛 BO2 第一场 8.16
2019/08/19 DOTA
Python HTTP客户端自定义Cookie实现实例
2017/04/28 Python
Jupyter notebook设置背景主题,字体大小及自动补全代码的操作
2020/04/13 Python
python 5个实用的技巧
2020/09/27 Python
如何让PyQt5中QWebEngineView与JavaScript交互
2020/10/21 Python
python复合条件下的字典排序
2020/12/18 Python
Python3利用scapy局域网实现自动多线程arp扫描功能
2021/01/21 Python
德购商城:德国进口直邮商城
2017/06/13 全球购物
施工资料员的岗位职责
2013/12/22 职场文书
我的大学生活职业生涯规划
2014/01/02 职场文书
销售主管岗位职责
2014/02/08 职场文书
中学生打架检讨书
2014/02/10 职场文书
公司成本主管岗位责任制
2014/02/21 职场文书
最经典的大学生职业生涯规划范文
2014/03/05 职场文书
校本教研活动总结
2014/07/01 职场文书
学校党员干部承诺书
2015/05/04 职场文书
校友会致辞
2015/07/30 职场文书
小学三年级班主任工作经验交流材料
2015/11/02 职场文书
施工安全协议书
2016/03/22 职场文书
pycharm无法导入lxml的解决办法
2021/03/31 Python
我对PyTorch dataloader里的shuffle=True的理解
2021/05/20 Python