ウェブサイト訪問情報統計用のphp

*文
リリース: 2023-03-18 14:16:02
オリジナル
2603 人が閲覧しました

PHP はどのようにして Web サイト訪問情報の統計を収集しますか?この記事では主に、PHP で Compete API を使用して Web サイトのアクセス情報統計を取得する方法を紹介し、PHP で Curl を使用して Compete Web サイトの統計情報を取得する手法を分析します。お役に立てれば幸いです。

この記事の例では、PHP で Compete API を使用して Web サイトのアクセス統計を取得する方法について説明します。皆さんの参考に共有してください。詳細は次のとおりです:

ここでは、Web サイトのアクセス統計を取得するために php を使用します。 Compete API は、Web サイトの情報をカウントするために特別に使用される Web サイトです


<?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);
 }
}
ログイン後にコピー

メールの開封数

ページビューをカウントするためのphpコード(テキストキャッシュ)​​

phpは、オンラインユーザー、今日の訪問者、合計訪問者をカウントします

以上がウェブサイト訪問情報統計用のphpの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!