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代码的53条建议
Mar 27 PHP
php网站来路获取代码(针对搜索引擎)
Jun 08 PHP
PHP计划任务之关闭浏览器后仍然继续执行的函数
Jul 22 PHP
php json_encode奇怪问题说明
Sep 27 PHP
php模拟post行为代码总结(POST方式不是绝对安全)
Feb 22 PHP
编写php应用程序实现摘要式身份验证的方法详解
Jun 08 PHP
codeigniter使用技巧批量插入数据实例方法分享
Dec 31 PHP
php使用数组填充下拉列表框的方法
Mar 31 PHP
PHP实现支付宝即时到账功能
Dec 21 PHP
thinkPHP框架动态配置用法实例分析
Jun 14 PHP
laravel框架 api自定义全局异常处理方法
Oct 11 PHP
Laravel框架自定义分页样式操作示例
Jan 26 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
第二节--PHP5 的对象模型
2006/11/16 PHP
HR vs ForZe BO3 第二场 2.13
2021/03/10 DOTA
Flash+XML滚动新闻代码 无图片 附源码下载
2007/11/22 Javascript
Jquery中增加参数与Json转换代码
2009/11/20 Javascript
javascript中&quot;/&quot;运算符常见错误
2010/10/13 Javascript
Javascript自定义函数判断网站访问类型是PC还是移动终端
2014/01/10 Javascript
Jquery自定义button按钮的几种方法
2014/06/11 Javascript
JS对象与json字符串格式转换实例
2014/10/28 Javascript
JavaScript中的console.dir()函数介绍
2014/12/29 Javascript
jquery遍历函数siblings()用法实例
2015/12/24 Javascript
JavaScript数组的一些奇葩行为
2016/01/25 Javascript
js绘制购物车抛物线动画
2020/11/18 Javascript
easyui datagrid 大数据加载效率慢,优化解决方法(推荐)
2016/11/09 Javascript
解决前端跨域问题方案汇总
2016/11/20 Javascript
Bootstrap modal 多弹窗之叠加关闭阴影遮罩问题的解决方法
2017/02/27 Javascript
浅谈vux之x-input使用以及源码解读
2018/11/04 Javascript
Python程序设计入门(3)数组的使用
2014/06/16 Python
Python获取Windows或Linux主机名称通用函数分享
2014/11/22 Python
简单介绍Ruby中的CGI编程
2015/04/10 Python
简单介绍Python中的round()方法
2015/05/15 Python
如何使用七牛Python SDK写一个同步脚本及使用教程
2015/08/23 Python
Python小白必备的8个最常用的内置函数(推荐)
2019/04/03 Python
python英语单词测试小程序代码实例
2019/09/09 Python
利用python 下载bilibili视频
2020/11/13 Python
Django中的DateTimeField和DateField实现
2021/02/24 Python
Html5剪切板功能的实现代码
2018/06/29 HTML / CSS
英国川宁茶官方网站:Twinings茶
2019/05/21 全球购物
小学生期末自我鉴定
2014/01/19 职场文书
策划创业计划书
2014/02/06 职场文书
网页美工求职信范文
2014/04/17 职场文书
森林病虫害防治方案
2014/06/02 职场文书
破坏寝室公物检讨书
2014/11/17 职场文书
党员争先创优承诺书
2015/01/20 职场文书
看看如何用Python绘制小米新版天价logo
2021/04/20 Python
python脚本框架webpy模板赋值实现
2021/11/20 Python
SQL Server携程核心系统无感迁移到MySQL实战
2022/06/01 SQL Server