PHP using curl to get compete statistics website information method

Source: Internet
Author: User
This article mainly introduces the PHP Access statistics class compete API usage, example analysis of PHP using curl to obtain compete statistics website information skills, with a certain reference value, the need for friends can refer to the next

The example in this article is about PHP's access to the Web site statistics class compete API usage. Specific as follows:

This is where PHP gets the website Access statistics class compete Api,compete is a website dedicated to statistics on website information

<?php//Check for Dependenciesif (!function_exists (' curl_init ')) throw new Exception (' Compete needs the curl PHP Extens Ion. '); 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{/** * De  Fault 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 basic ' PV ',//Page views all-access ' avg Stay ',//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 ', ' Pagevi EWS ' = = ' PV ', ' averagestay ' = ' avgstay ', ' visitsperson ' = ' VPP ', ' pagesvisit ' = ' ppv ', ' attention ' = = ' att ', ' dailyreach ' = ' reachd ', ' dailyattention ' = ' attd ', ' gender ' = ' gen ', ' age ' =& Gt ' 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])) r  Eturn $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->_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);}/** * Exe  Cute 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); }}

Summary : The above is the entire content of this article, I hope to be able to help you learn.

Related recommendations:

PHP sends post, get requests, and action cookies based on Curl

PHP Query the function of songs through the Sina Library search interface

PHP dynamic creation of folders and file upload method

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.