| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314 |
- <?php
- namespace addons\baiduwebtongji\library;
- use addons\baiduwebtongji\library\ReportService;
- /**
- * 百度网站统计api
- */
- class TongjiApi {
- protected $domain;
- protected $siteId;
- /**
- * 参数赋值、登录获取siteId
- */
- public function __construct($config)
- {
- // define('LOGIN_URL', 'https://api.baidu.com/sem/common/HolmesLoginService');
- // define('API_URL', 'https://api.baidu.com/json/tongji/v1/ReportService');
- define('ACCOUNT_TYPE', $config['account_type']);
- // $this->domain=$config['domain'];
- $this->config=$config;
- $this->siteId=$config['siteId'];
- }
- /**
- * 获取数据
- * @param string $method 查询方法
- * @param string $mertics 查询参数
- * @param string $order 排序
- */
- private function getData($param)
- {
- $reportService = new ReportService($this->config);
- // $reportService = new ReportService(API_URL, USERNAME,TOKEN,PASSWORD);
- if(!$this->siteId){
- return false;
- // $ret = $reportService->getSiteList();
- // $siteList = $ret['body']['data'][0]['list'];
- // if($siteList){
- // if (count($siteList) > 0) {
- // if($this->domain){
- // foreach ($siteList as $key => $value) {
- // if($this->domain==$value["domain"]){
- // $this->siteId=$value["site_id"];
- // break;
- // }else{
- // continue;
- // }
- // }
- // if(!$this->siteId){
- // $this->siteId = $siteList[0]['site_id'];
- // }
- // }else{
- // $this->siteId = $siteList[0]['site_id'];
- // }
- // }else{
- // return false;
- // }
- // }else{
- // return false;
- // }
- }
- $param['site_id']=$this->siteId;
- $res = $reportService->getData($param);
- if(ACCOUNT_TYPE==3){
- return $res;
- }else{
- return json_decode($res['raw'],true);
- }
- }
- public function getSiteList(){
- // $reportService = new ReportService(API_URL, USERNAME,TOKEN,PASSWORD);
- $reportService = new ReportService($this->config);
- $ret = $reportService->getSiteList();
- if(ACCOUNT_TYPE==3){
- $siteList = $ret['list'];
- }else{
- $siteList = $ret['body']['data'][0]['list'];
- }
- $data=[];
- if($siteList){
- foreach ($siteList as $key => $value) {
- $data[]=[
- 'site_id'=>$value["site_id"],
- 'domain'=>$value["domain"]
- ];
- }
- }
- return $data;
- }
- /**
- * 获取网站今日流量与昨日对比
- */
- public function getOutline()
- {
- $param=[
- 'method' => 'overview/getOutline',
- ];
- return $this->getData($param);
- }
- /**
- * 获取今日趋势分析数据
- */
- public function trendTimeA()
- {
- $param=[
- 'method' => 'trend/time/a',
- 'metrics' => 'pv_count,visitor_count',//,ip_count,bounce_ratio,avg_visit_time
- 'start_date'=> date("Ymd"),
- 'end_date' => date("Ymd"),
- 'order' => 'simple_date_title,asc'
- ];
- $res=$this->getData($param);
- return $res;
- }
- /**
- * [getTimeTrendRpt 网站概况]
- * @return [type] [description]
- */
- public function getTimeTrendRpt($start_date,$end_date){
- $param=[
- 'method' => 'overview/getTimeTrendRpt',
- 'metrics' => 'pv_count,visitor_count',
- 'start_date' => $start_date,
- 'end_date' => $end_date,
- ];
- return $this->getData($param);
- }
- /**
- * 获取访问地域分布数据
- */
- public function getDistrictRpt($start_date,$end_date)
- {
- $param=[
- 'method'=>'overview/getDistrictRpt',
- 'metrics'=>'pv_count,visitor_count',
- 'start_date'=> $start_date,
- 'end_date' => $end_date,
- ];
- return $this->getData($param);
- }
- /**
- * 获取访问搜索词、来源网站、入口界面、受访页面、新老访客数据
- */
- public function getCommonTrackRpt($start_date,$end_date)
- {
- $param=[
- 'method'=>'overview/getCommonTrackRpt',
- 'metrics'=>'pv_count',
- 'start_date'=> $start_date,
- 'end_date' => $end_date,
- ];
- return $this->getData($param);
- }
- /**
- * 实时访客
- * @param [int] $page [页码]
- * @param [int] $max_results [每页数量]
- * @return [array] [description]
- */
- public function trendLatestA($page,$max_results){
- $start_index=($page-1)*$max_results;
- $param=[
- 'method' => 'trend/latest/a',
- 'metrics' => 'start_time,area,source,access_page,keyword,searchword,ip,visit_time,visit_pages',
- 'max_results' => $max_results,
- 'start_index' => $start_index
- ];
- return $this->getData($param);
- }
- /**
- * [sourceAll 全部来源]
- * @param [int] $page [页码]
- * @param [int] $max_results [每页数量]
- * @param [date] $start_date [开始时间]
- * @param [date] $end_date [结束时间]
- * @return [array] [description]
- */
- public function sourceAll($page,$max_results,$start_date,$end_date){
- $start_index=($page-1)*$max_results;
- $param=[
- 'method' => 'source/all/a',
- 'metrics' => 'pv_count,visitor_count,ip_count,bounce_ratio,avg_visit_time',
- 'viewType' => 'site',
- 'max_results' => $max_results,
- 'start_index' => $start_index,
- 'start_date' => $start_date,
- 'end_date' => $end_date,
- ];
- return $this->getData($param);
- }
- /**
- * [sourceSearchword 搜索词]
- * @param [int] $page [页码]
- * @param [int] $max_results [每页数量]
- * @param [date] $start_date [开始时间]
- * @param [date] $end_date [结束时间]
- * @return [array] [description]
- */
- public function sourceSearchword($page,$max_results,$start_date,$end_date){
- $start_index=($page-1)*$max_results;
- $param=[
- 'method' => 'source/searchword/a',
- 'metrics' => 'pv_count,visitor_count,ip_count,bounce_ratio,avg_visit_time',
- 'viewType' => 'site',
- 'max_results' => $max_results,
- 'start_index' => $start_index,
- 'start_date' => $start_date,
- 'end_date' => $end_date,
- ];
- return $this->getData($param);
- }
- /**
- * [visitToppage 受访页面]
- * @param [int] $page [页码]
- * @param [int] $max_results [每页数量]
- * @param [date] $start_date [开始时间]
- * @param [date] $end_date [结束时间]
- * @return [array] [description]
- */
- public function visitToppage($page,$max_results,$start_date,$end_date){
- $start_index=($page-1)*$max_results;
- $param=[
- 'method' => 'visit/toppage/a',
- 'metrics' => 'pv_count,visitor_count,ip_count,visit1_count,outward_count,exit_count,exit_ratio,average_stay_time',
- 'max_results' => $max_results,
- 'start_index' => $start_index,
- 'start_date' => $start_date,
- 'end_date' => $end_date,
- ];
- return $this->getData($param);
- }
- /**
- * [visitDistrict 地域分布(按省)]
- * @param [int] $page [页码]
- * @param [int] $max_results [每页数量]
- * @param [date] $start_date [开始时间]
- * @param [date] $end_date [结束时间]
- * @return [array] [description]
- */
- public function visitDistrict($param){
- $param['method']='visit/district/a';
- return $this->getData($param);
- }
- /**
- * [visitDistrictTop 地域分布(按城市)]
- * @param [type] $start_date [开始时间]
- * @param [type] $end_date [结束时间]
- * @param [type] $area [省份]
- * @return [type] [description]
- */
- public function visitDistrictTop($start_date,$end_date,$area){
- $param=[
- 'method' => 'visit/district/top',
- 'metrics' => 'pv_count,visitor_count,ip_count,bounce_ratio,avg_visit_time',
- 'start_date' => $start_date,
- 'end_date' => $end_date,
- 'viewType' => 'city',
- 'area' => $area
- ];
- return $this->getData($param);
- }
- /**
- * [visitWorld 地域分布(按国家)]
- * @param [int] $page [页码]
- * @param [int] $max_results [每页数量]
- * @param [date] $start_date [开始时间]
- * @param [date] $end_date [结束时间]
- * @return [array] [description]
- */
- public function visitWorld($start_date,$end_date){
- $param=[
- 'method' => 'visit/world/a',
- 'metrics' => 'pv_count,visitor_count,ip_count,bounce_ratio,avg_visit_time',
- 'start_date' => $start_date,
- 'end_date' => $end_date,
- ];
- return $this->getData($param);
- }
- public function rankGetData($start_date,$end_date){
- $param=[
- 'method' => 'opt/searchwordrank/getData',
- 'metrics' => '',
- 'start_date' => $start_date,
- 'end_date' => $end_date,
- 'viewType' => 'searchword_rank'
- ];
- return $this->getData($param);
- }
- public function rankGetWords(){
- $param=[
- 'method' => 'opt/searchwordrank/getWords',
- ];
- return $this->getData($param);
- }
- public function rankSetWords($words){
- $param=[
- 'method' => 'opt/searchwordrank/setWords',
- 'words' => $words
- ];
- return $this->getData($param);
- }
- public function reportSend($param){
- $param['method'] = 'home/reportsend/add';
- return $this->getData($param);
- }
- }
|