php获得网站访问统计信息类Compete API用法实例
本文实例讲述了php获得网站访问统计信息类CompeteAPI用法。分享给大家供大家参考。具体如下:
这里使用php获得网站访问统计信息类CompeteAPI,Compete是一个专门用来统计网站信息的网站
<?php //Checkfordependencies if(!function_exists('curl_init')) thrownewException('CompeteneedstheCURLPHPextension.'); if(!function_exists('json_decode')) thrownewException('CompeteneedstheJSONPHPextension.'); /** *BaseCompeteexceptionclass. */ classCompeteExceptionextendsException{} /** *RepresentsCompeteAPI. *@authorEgorGumenyuk(boo1ean0807atgmaildotcom) *@packageCompete *@licenseApache2.0 */ classCompete { /** *Defaultusragent. */ constUSER_AGENT='CompeteAPIwrapperforPHP'; /** *Baseurlforapicalls. */ constAPI_BASE_URL='http://apps.compete.com/sites/:domain/trended/:metric/?apikey=:key'; /** *Masksforurlparams. */ private$_urlKeys=array(':domain',':metric',':key'); private$_apiKey; /** *Forurlcleaning. */ private$_toSearch=array('http://','www.'); private$_toReplace=array('',''); /** *Listofavailablemetrics. */ private$_availableMetrics=array( //DescriptionAuthtype 'uv',//UniqueVisitorsBasic 'vis',//VisitsBasic 'rank',//RankBasic 'pv',//PageViewsAll-Access 'avgstay',//AverageStayAll-Access 'vpp',//Visits/PersonAll-Access 'ppv',//Pages/VisitAll-Access 'att',//AttentionAll-Access 'reachd',//DailyReachAll-Access 'attd',//DailyAttentionAll-Access 'gen',//GenderAll-Access 'age',//AgeAll-Access 'inc',//IncomeAll-Access ); /** *Listofavailablemethodsfor__call()implementation. */ private$_metrics=array( 'uniqueVisitors'=>'uv', 'visits'=>'vis', 'rank'=>'rank', 'pageViews'=>'pv', 'averageStay'=>'avgstay', 'visitsPerson'=>'vpp', 'pagesVisit'=>'ppv', 'attention'=>'att', 'dailyReach'=>'reachd', 'dailyAttention'=>'attd', 'gender'=>'gen', 'age'=>'age', 'income'=>'inc' ); /** *CreateaccesstoCompeteAPI. *@paramstring$apiKeyuser'sapikey. */ publicfunction__construct($apiKey){ $this->_apiKey=$apiKey; } /** *Implementspecificmethods. */ publicfunction__call($name,$args){ if(array_key_exists($name,$this->_metrics)&&isset($args[0])) return$this->get($args[0],$this->_metrics[$name]); thrownewCompeteException($name.'methoddoesnotexist.'); } /** *GetdatafromCompete. *@paramstring$sitesomedomain. *@paramstring$metricmetrictoget. *@returnstdClassCompetedata. *@throwsCompeteException */ publicfunctionget($site,$metric){ if(!in_array($metric,$this->_availableMetrics)) thrownewCompeteException($metric.'-wrongmetric.'); $values=array( $this->_prepareUrl($site), $metric, $this->_apiKey ); //Preparecallurl $url=str_replace($this->_urlKeys,$values,self::API_BASE_URL); //RetrievedatausingHTTPGETmethod. $data=json_decode($this->_get($url)); //Becauseofunsuccessfulresponsescontain"status_message". if(!isset($data->status_message)) return$data; thrownewCompeteException('Status:'.$data->status.'.'.$data->status_message); } /** *Cutunnecessarypartsofurl. *@paramstring$urlsomeurl. *@returnstringtrimmedurl. */ privatefunction_prepareUrl($url){ returnstr_replace($this->_toSearch,$this->_toReplace,$url); } /** *Executehttpgetmethod. *@paramstring$urlrequesturl. *@returnstringresponse. */ privatefunction_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); returncurl_exec($ch); } }
希望本文所述对大家的php程序设计有所帮助。