Heim  >  Artikel  >  Backend-Entwicklung  >  PHP für Website-Besuchsinformationsstatistiken

PHP für Website-Besuchsinformationsstatistiken

*文
*文Original
2017-12-26 12:07:552603Durchsuche

Wie erfasst PHP Statistiken zu Website-Besuchen? In diesem Artikel wird hauptsächlich die Verwendung der Compete-API in PHP zum Abrufen von Statistiken zu Website-Zugriffsinformationen vorgestellt und die Techniken zur Verwendung von Curl in PHP zum Abrufen statistischer Website-Informationen von Compete analysiert. Ich hoffe, es hilft allen.

Das Beispiel in diesem Artikel beschreibt die Verwendung der Compete API in PHP, um Website-Zugriffsstatistiken zu erhalten. Teilen Sie es als Referenz mit allen. Die Details lauten wie folgt:

Hier wird PHP verwendet, um Website-Zugriffsstatistiken zu erhalten. Compete API ist eine Website, die speziell zum Zählen von Website-Informationen verwendet wird


<?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);
 }
}

Verwandte Empfehlungen:

img src php zählt E-Mail-Öffnungszeiten

php zählt Seitenaufrufe Anzahl der Male (Textcache) Code

php zählt Online-Personen, heutige Besuche, Gesamtbesuche

Das obige ist der detaillierte Inhalt vonPHP für Website-Besuchsinformationsstatistiken. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn