This article illustrates the use of PHP to obtain Web Access statistics class compete API. Share to everyone for your reference. Specifically as follows:
Here use PHP to get the site access statistics class compete api,compete is a website that is designed to be used to count website information.
<?php//Check for dependencies if (!function_exists (' curl_init ')) throw new Exception (' Compete needs the curl PHP ex
Tension. ');
if (!function_exists (' Json_decode ')) throw new Exception (' Compete needs the JSON PHP extension. ');
/** * 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 = ' Compete API wrapper for PHP ';
/** * Base URL for API calls.
* * Const API_BASE_URL = ' http://apps.compete.com/sites/:d omain/trended/:metric/?apikey=:key ';
/** * Masks for URL params.
* Private $_urlkeys = Array (':d omain ', ': Metric ', ': Key ');
Private $_apikey;
/** * for URL cleaning.
* Private $_tosearch = array (' http://', ' www. ');
Private $_toreplace = Array (', ');
/** * List of available metrics.
* Private $_availablemetrics = Array (Description Auth type ' UV ',//Unique visitors basic ' vis ',//Visits basic ' rank ',//Rank Basi
C ' PV ',//Page views all-access ' Avgstay ',//Average Stay all-access ' VPP ',//Visits/person all-access ' PPV ',///pages/visit all-access ' att ',//Attention all-access ' reachd ',//daily reach all-access ' Attd ', Daily Attention All-access ' gen ',//Gender all-access ' age ',//Age all-access ' Inc ',//income
all-access);
/** * List of available methods for __call () implementation. * Private $_metrics = Array (' uniquevisitors ' => ' UV ', ' visits ' => ' vis ', ' rank ' => ' rank ', ' p Ageviews ' => ' PV ', ' Averagestay ' => ' avgstay ', ' Visitsperson ' => ' VPP ', ' pagesvisit ' => ' PPV ', '
Attention ' => ' att ', ' dailyreach ' => ' reachd ', ' dailyattention ' => ' ' attd ', ' gender ' => ' gen ',
' Age ' => ' age ', ' Income ' => ' Inc ');
/** * Create access to Compete API.
* @param string $apiKey user ' 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.
' method does not exist ');
}/** * 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->_availablemetric s) throw new Competeexception ($metric.
'-wrong metric. ');
$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 (' Status: '. $data->status. '.
'. $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); }
}
I hope this article will help you with your PHP programming.