php使用curl取得Compete統計網站資訊的方法

墨辰丷
發布: 2023-03-31 16:14:02
原創
1398 人瀏覽過

這篇文章主要介紹了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);
 }
}
登入後複製

總結:以上就是本篇文的全部內容,希望對大家的學習有所幫助。

相關推薦:

PHP基於curl發送post、get請求及操作cookie

php透過新浪樂庫搜尋介面查詢歌曲的功能

php動態建立資料夾及檔案上傳的方法

以上是php使用curl取得Compete統計網站資訊的方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!