| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- <?php
- namespace addons\baiduwebtongji\library;
- use addons\baiduwebtongji\library\DataApiConnection;
- /**
- * class ReportService
- */
- /**
- * ReportService
- */
- class ReportService {
- private $apiUrl;
- private $userName;
- private $token;
- private $ucid;
- private $password;
-
- /**
- * construct
- * @param string $apiUrl
- * @param string $userName
- * @param string $token
- * @param string $ucid
- * @param string $password
- */
- public function __construct($config) {
- define('UUID', $config['uuid']);
- $this->config=$config;
- if(ACCOUNT_TYPE==1 || ACCOUNT_TYPE==2){
- $config_url=$report_url="https://api.baidu.com/json/tongji/v1/ReportService";
- if(ACCOUNT_TYPE==1){
- $this->header=[
- 'username' => $config['username'],
- 'password' => $config['password'],
- 'token' => $config['token'],
- 'account_type' => ACCOUNT_TYPE,
- ];
- }elseif(ACCOUNT_TYPE==2){
- $this->header=[
- 'userName' => $config['username'],
- 'accessToken' => $config['token']
- ];
- }
- }elseif(ACCOUNT_TYPE==3){
- $config_url="https://openapi.baidu.com/rest/2.0/tongji/config/getSiteList?access_token=".$config['token'];
- $report_url="https://openapi.baidu.com/rest/2.0/tongji/report/getData?access_token=".$config['token'];
- }
- $this->config_url = $config_url;
- $this->report_url = $report_url;
- }
-
- /**
- * get site list
- * @return array
- */
- public function getSiteList() {
- if(ACCOUNT_TYPE==3){
- $res=json_decode(file_get_contents($this->config_url),true);
- if(isset($res['error_code'])){
- $tokenArr=json_decode(file_get_contents("https://openapi.baidu.com/oauth/2.0/token?grant_type=refresh_token&refresh_token=".$this->config['refresh_token']."&client_id=".$this->config['apikey']."&client_secret=".$this->config['secretkey']),true);
- set_addon_config("baiduwebtongji",['token'=>$tokenArr['access_token'],'refresh_token'=>$tokenArr['refresh_token']]);
- $res=json_decode(file_get_contents("https://openapi.baidu.com/rest/2.0/tongji/config/getSiteList?access_token=".$tokenArr['access_token']),true);
- }
- return $res;
- }else{
- $apiConnection = new DataApiConnection();
- $apiConnection->init($this->config_url . '/getSiteList');
- $apiConnectionData = array(
- 'header' => $this->header,
- 'body' => null,
- );
- $apiConnection->POST($apiConnectionData);
-
- return array(
- 'header' => $apiConnection->retHead,
- 'body' => $apiConnection->retBody,
- 'raw' => $apiConnection->retRaw,
- );
- }
- }
- /**
- * get data
- * @param array $parameters
- * @return array
- */
- public function getData($parameters) {
- if(ACCOUNT_TYPE==3){
- $str=[];
- foreach ($parameters as $key => $value) {
- $str[]=$key."=".$value;
- }
- $str=implode("&",$str);
- $url=$this->report_url."&".$str;
- $res=json_decode(file_get_contents($url),true);
- return $res;
- }else{
- $apiConnection = new DataApiConnection();
- $apiConnection->init($this->report_url . '/getData');
- $apiConnectionData = array(
- 'header' => $this->header,
- 'body' => $parameters,
- );
- $apiConnection->POST($apiConnectionData);
-
- return array(
- 'header' => $apiConnection->retHead,
- 'body' => $apiConnection->retBody,
- 'raw' => $apiConnection->retRaw,
- );
- }
- }
- }
|