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, ); } } }