首页 > 开发 > PHP > 正文

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

2024-05-04 23:33:48
字体:大 中 小
来源:转载
供稿:网友

这篇文章主要介绍了php获得网站访问统计信息类Compete API用法,实例分析了php使用curl获取Compete统计网站信息的技巧,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了php获得网站访问统计信息类Compete API用法。分享给大家供大家参考。具体如下:

这里使用php获得网站访问统计信息类Compete API,Compete是一个专门用来统计网站信息的网站

 

 
  1. <?php 
  2. // Check for dependencies 
  3. if (!function_exists('curl_init')) 
  4. throw new Exception('Compete needs the CURL PHP extension.'); 
  5. if (!function_exists('json_decode')) 
  6. throw new Exception('Compete needs the JSON PHP extension.'); 
  7. /** 
  8. * Base Compete exception class. 
  9. */ 
  10. class CompeteException extends Exception {} 
  11. /** 
  12. * Represents Compete API. 
  13. * @author Egor Gumenyuk (boo1ean0807 at gmail dot com) 
  14. * @package Compete 
  15. * @license Apache 2.0 
  16. */ 
  17. class Compete 
  18. { 
  19. /** 
  20. * Default usr agent. 
  21. */ 
  22. const USER_AGENT = 'Compete API wrapper for PHP'; 
  23. /** 
  24. * Base url for api calls. 
  25. */ 
  26. const API_BASE_URL = 'http://apps.compete.com/sites/:domain/trended/:metric/?apikey=:key'; 
  27. /** 
  28. * Masks for url params. 
  29. */ 
  30. private $_urlKeys = array(':domain', ':metric', ':key'); 
  31. private $_apiKey; 
  32. /** 
  33. * For url cleaning. 
  34. */ 
  35. private $_toSearch = array('http://', 'www.'); 
  36. private $_toReplace = array('', ''); 
  37. /** 
  38. * List of available metrics. 
  39. */ 
  40. private $_availableMetrics = array( 
  41. // Description Auth type 
  42. 'uv', // Unique Visitors Basic 
  43. 'vis', // Visits Basic 
  44. 'rank', // Rank Basic 
  45. 'pv', // Page Views All-Access 
  46. 'avgstay',// Average Stay All-Access 
  47. 'vpp', // Visits/Person All-Access 
  48. 'ppv', // Pages/Visit All-Access 
  49. 'att', // Attention All-Access 
  50. 'reachd', // Daily Reach All-Access 
  51. 'attd', // Daily Attention All-Access 
  52. 'gen', // Gender All-Access 
  53. 'age', // Age All-Access 
  54. 'inc', // Income All-Access 
  55. ); 
  56. /** 
  57. * List of available methods for __call() implementation. 
  58. */ 
  59. private $_metrics = array( 
  60. 'uniqueVisitors' => 'uv', 
  61. 'visits' => 'vis', 
  62. 'rank' => 'rank', 
  63. 'pageViews' => 'pv', 
  64. 'averageStay' => 'avgstay', 
  65. 'visitsPerson' => 'vpp', 
  66. 'pagesVisit' => 'ppv', 
  67. 'attention' => 'att', 
  68. 'dailyReach' => 'reachd', 
  69. 'dailyAttention' => 'attd', 
  70. 'gender' => 'gen', 
  71. 'age' => 'age', 
  72. 'income' => 'inc' 
  73. ); 
  74. /** 
  75. * Create access to Compete API. 
  76. * @param string $apiKey user's api key. 
  77. */ 
  78. public function __construct($apiKey) { 
  79. $this->_apiKey = $apiKey; 
  80. } 
  81. /** 
  82. * Implement specific methods. 
  83. */ 
  84. public function __call($name, $args) { 
  85. if (array_key_exists($name, $this->_metrics) && isset($args[0])) 
  86. return $this->get($args[0], $this->_metrics[$name]); 
  87. throw new CompeteException($name . ' method does not exist.'); 
  88. } 
  89. /** 
  90. * Get data from Compete. 
  91. * @param string $site some domain. 
  92. * @param string $metric metric to get. 
  93. * @return stdClass Compete data. 
  94. * @throws CompeteException 
  95. */ 
  96. public function get($site, $metric) { 
  97. if (!in_array($metric, $this->_availableMetrics)) 
  98. throw new CompeteException($metric . ' - wrong metric.'); 
  99. $values = array( 
  100. $this->_prepareUrl($site), 
  101. $metric, 
  102. $this->_apiKey 
  103. ); 
  104. // Prepare call url 
  105. $url = str_replace($this->_urlKeys, $values, self::API_BASE_URL); 
  106. // Retrieve data using HTTP GET method. 
  107. $data = json_decode($this->_get($url)); 
  108. // Because of unsuccessful responses contain "status_message". 
  109. if (!isset($data->status_message)) 
  110. return $data; 
  111. throw new CompeteException('Status: ' . $data->status . '. ' .$data->status_message); 
  112. } 
  113. /** 
  114. * Cut unnecessary parts of url. 
  115. * @param string $url some url. 
  116. * @return string trimmed url. 
  117. */ 
  118. private function _prepareUrl($url) { 
  119. return str_replace($this->_toSearch, $this->_toReplace, $url); 
  120. } 
  121. /** 
  122. * Execute http get method. 
  123. * @param string $url request url. 
  124. * @return string response. 
  125. */ 
  126. private function _get($url) { 
  127. $ch = curl_init(); 
  128. curl_setopt($ch, CURLOPT_URL, $url); 
  129. curl_setopt($ch, CURLOPT_USERAGENT, self::USER_AGENT); 
  130. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
  131. return curl_exec($ch); 
  132. } 
  133. } 

希望本文所述对大家的php程序设计有所帮助。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表