首頁  >  文章  >  後端開發  >  php進行網站存取資訊統計

php進行網站存取資訊統計

*文
*文原創
2017-12-26 12:07:552603瀏覽

php如何進行網站存取資訊統計?本文主要介紹了php取得網站存取資訊統計類別Compete API用法,實例分析了php使用curl取得Compete統計網站資訊的技巧。希望對大家有幫助。

本文實例講述了php取得網站存取統計資訊類別Compete API用法。分享給大家供大家參考。具體如下:

這裡使用php取得網站存取統計資訊類別Compete API,Compete是一個專門用來統計網站資訊的網站


<?php
// Check for dependencies
if (!function_exists(&#39;curl_init&#39;))
 throw new Exception(&#39;Compete needs the CURL PHP extension.&#39;);
if (!function_exists(&#39;json_decode&#39;))
 throw new Exception(&#39;Compete needs the JSON PHP extension.&#39;);
/**
 * 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  = &#39;Compete API wrapper for PHP&#39;;
 /**
  * Base url for api calls.
  */
 const API_BASE_URL = &#39;http://apps.compete.com/sites/:domain/trended/:metric/?apikey=:key&#39;;
 /**
  * Masks for url params.
  */
 private $_urlKeys = array(&#39;:domain&#39;, &#39;:metric&#39;, &#39;:key&#39;);
 private $_apiKey;
 /**
  * For url cleaning.
  */
 private $_toSearch = array(&#39;http://&#39;, &#39;www.&#39;);
 private $_toReplace = array(&#39;&#39;, &#39;&#39;);
 /**
  * List of available metrics.
  */
 private $_availableMetrics = array(
       // Description   Auth type
  &#39;uv&#39;,   // Unique Visitors Basic
  &#39;vis&#39;,  // Visits      Basic
  &#39;rank&#39;,  // Rank       Basic
  &#39;pv&#39;,   // Page Views    All-Access
  &#39;avgstay&#39;,// Average Stay   All-Access
  &#39;vpp&#39;,  // Visits/Person  All-Access
  &#39;ppv&#39;,  // Pages/Visit   All-Access
  &#39;att&#39;,  // Attention    All-Access
  &#39;reachd&#39;, // Daily Reach   All-Access
  &#39;attd&#39;,  // Daily Attention All-Access
  &#39;gen&#39;,  // Gender      All-Access
  &#39;age&#39;,  // Age       All-Access
  &#39;inc&#39;,  // Income      All-Access
 );
 /**
  * List of available methods for __call() implementation.
  */
 private $_metrics = array(
  &#39;uniqueVisitors&#39; => &#39;uv&#39;,
  &#39;visits&#39;     => &#39;vis&#39;,
  &#39;rank&#39;      => &#39;rank&#39;,
  &#39;pageViews&#39;   => &#39;pv&#39;,
  &#39;averageStay&#39;  => &#39;avgstay&#39;,
  &#39;visitsPerson&#39;  => &#39;vpp&#39;,
  &#39;pagesVisit&#39;   => &#39;ppv&#39;,
  &#39;attention&#39;   => &#39;att&#39;,
  &#39;dailyReach&#39;   => &#39;reachd&#39;,
  &#39;dailyAttention&#39; => &#39;attd&#39;,
  &#39;gender&#39;     => &#39;gen&#39;,
  &#39;age&#39;      => &#39;age&#39;,
  &#39;income&#39;     => &#39;inc&#39;
 );
 /**
  * Create access to Compete API.
  * @param string $apiKey user&#39;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 . &#39; method does not exist.&#39;);
 }
 /**
  * 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 . &#39; - wrong metric.&#39;);
  $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(&#39;Status: &#39; . $data->status . &#39;. &#39; .$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);
 }
}

#相關推薦:

img src php 統計郵件開啟次數

php 統計頁面瀏覽次數(文字快取)的程式碼

php 統計線上人數,今日造訪量,總造訪量

以上是php進行網站存取資訊統計的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn