Example of CompeteAPI usage for php Website access statistics

Source: Internet
Author: User
This article mainly introduces the usage of CompeteAPI in the php Website access statistics class. The example analyzes php's skills in using curl to obtain Compete website information, which has some reference value. If you need it, you can refer

This article mainly introduces the usage of Compete API in the php Website access statistics class. The example analyzes php's skills in using curl to obtain Compete website information, which has some reference value. If you need it, you can refer

This example describes the usage of the Compete API used by php to obtain Website access statistics. Share it with you for your reference. The details are as follows:

Here, we use php to obtain the Website access statistics class Compete API. Compete is a website dedicated to collecting website information.

<? Php // Check for dependenciesif (! Function_exists ('curl _ init ') throw new Exception ('compete needs the curl PHP extension.'); 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 CILS. */const API_BASE_URL =' http://apps.compete.com/sites/:domain/trended/:metric/?apikey=:key ';/*** Masks for url params. */private $ _ urlKeys = array (': domain', ': 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 ', // VisitsBasic 'rank ', // RankBasic 'pv', // Pa Ge Views All-Access 'avstay', // Average Stay All-Access 'vps', // Visits/Person All-Access 'vps ', // Pages/Visit All-Access 'att ', // Attention All-Access 'reachd', // Daily Reach All-Access 'attd ', // Daily Attention All-Access 'gen', // GenderAll-Access 'age', // AgeAll-Access 'inc', // IncomeAll-Access ); /*** List of available methods for _ call () implementation. */private $ _ metrics = array ('Uniquevisitors '=> 'U', 'visits' => 'vis ', 'rank' => 'rank ', 'pageview' => 'pv ', 'averagestay' => 'avstay', 'visitsperson '=> 'vps', 'pagesvisit' => 'vppu', 'Attention' => 'att ', 'dailyad' => 'reachd', 'dailyattention '=> 'attd', 'gender' => 'gen', 'age' => 'age ', 'Welcome '=> 'inc');/*** Create access to Compete API. * @ param string $ apiKey user's api key. */public function _ construct ($ api Key) {$ 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 to get. * @ r Eturn stdClass Compete data. * @ throws CompeteException */public function get ($ site, $ metric) {if (! In_array ($ metric, $ this-> _ availableMetrics) 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 php programming.

,

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.