php获得网站访问统计信息类Compete API用法实例

 更新时间:2015年04月02日 15:17:07   作者:pythoner  
这篇文章主要介绍了php获得网站访问统计信息类Compete API用法,实例分析了php使用curl获取Compete统计网站信息的技巧,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了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多数据库支持的应用程序设计

    以前做PHP应用,多数是单数据库数据查询和更新,顶多也是主从数据库的支持,实现起来相对简单。主从数据库的问题在于,当会话存储在数据库的时候,同步将可能出现问题,也就是说有可能出现会话的中断。
    2008-08-08
  • PHP中include和require的使用详解

    PHP中include和require的使用详解

    这篇文章主要介绍了PHP中include和require的使用详解,帮助大家更好的理解和学习使用PHP,感兴趣的朋友可以了解下
    2021-04-04
  • PHP如何抛出异常处理错误

    PHP如何抛出异常处理错误

    PHP 5 提供了一种新的面向对象的错误处理方法。异常处理用于在指定的错误(异常)情况发生时改变脚本的正常流程。这种情况称为异常。
    2011-03-03
  • php用数组返回无限分类的列表数据的代码

    php用数组返回无限分类的列表数据的代码

    php自定义函数之用数组返回无限分类的列表数据,这样的实现可以提高执行的效率不要每次都从数据库读取数据。
    2010-08-08
  • 解析在PHP中使用全局变量的几种方法

    解析在PHP中使用全局变量的几种方法

    本篇文章是对在PHP中使用全局变量的几种方法进行了详细的分析介绍,需要的朋友参考下
    2013-06-06
  • PHP计算当前坐标3公里内4个角落的最大最小经纬度实例

    PHP计算当前坐标3公里内4个角落的最大最小经纬度实例

    这篇文章主要介绍了PHP计算当前坐标3公里内4个角落的最大最小经纬度的方法,涉及PHP数学运算的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2016-02-02
  • ThinkPHP 404页面的设置方法

    ThinkPHP 404页面的设置方法

    这篇文章主要介绍了ThinkPHP 404页面的设置方法,需要的朋友可以参考下
    2015-01-01
  • PHP令牌 Token改进版

    PHP令牌 Token改进版

    那个版本中,存在一个小问题,因为要做可逆加密,而加密出来的字符是不可显示字符+乱码,所以我用了 base64对其进行了处理,这样一来,就不会有乱码和不可显示字符了。
    2008-07-07
  • php_imagick实现图片剪切、旋转、锐化、减色或增加特效的方法

    php_imagick实现图片剪切、旋转、锐化、减色或增加特效的方法

    这篇文章主要介绍了php_imagick实现图片剪切、旋转、锐化、减色或增加特效的方法,可实现通过调用ImageMagick功能的PHP扩展使PHP具备和ImageMagick相同的功能,最终实现强大的ImageMagick图形处理功能,非常具有实用价值,需要的朋友可以参考下
    2014-12-12
  • PHP函数常用用法小结

    PHP函数常用用法小结

    由于PHP是一种解释型语言,所以函数编写和调用很是方便。总结一下魔术函数、嵌套函数、匿名函数、函数调用方式和函数参数使用等。
    2010-02-02

最新评论