TongjiApi.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. <?php
  2. namespace addons\baiduwebtongji\library;
  3. use addons\baiduwebtongji\library\ReportService;
  4. /**
  5. * 百度网站统计api
  6. */
  7. class TongjiApi {
  8. protected $domain;
  9. protected $siteId;
  10. /**
  11. * 参数赋值、登录获取siteId
  12. */
  13. public function __construct($config)
  14. {
  15. // define('LOGIN_URL', 'https://api.baidu.com/sem/common/HolmesLoginService');
  16. // define('API_URL', 'https://api.baidu.com/json/tongji/v1/ReportService');
  17. define('ACCOUNT_TYPE', $config['account_type']);
  18. // $this->domain=$config['domain'];
  19. $this->config=$config;
  20. $this->siteId=$config['siteId'];
  21. }
  22. /**
  23. * 获取数据
  24. * @param string $method 查询方法
  25. * @param string $mertics 查询参数
  26. * @param string $order 排序
  27. */
  28. private function getData($param)
  29. {
  30. $reportService = new ReportService($this->config);
  31. // $reportService = new ReportService(API_URL, USERNAME,TOKEN,PASSWORD);
  32. if(!$this->siteId){
  33. return false;
  34. // $ret = $reportService->getSiteList();
  35. // $siteList = $ret['body']['data'][0]['list'];
  36. // if($siteList){
  37. // if (count($siteList) > 0) {
  38. // if($this->domain){
  39. // foreach ($siteList as $key => $value) {
  40. // if($this->domain==$value["domain"]){
  41. // $this->siteId=$value["site_id"];
  42. // break;
  43. // }else{
  44. // continue;
  45. // }
  46. // }
  47. // if(!$this->siteId){
  48. // $this->siteId = $siteList[0]['site_id'];
  49. // }
  50. // }else{
  51. // $this->siteId = $siteList[0]['site_id'];
  52. // }
  53. // }else{
  54. // return false;
  55. // }
  56. // }else{
  57. // return false;
  58. // }
  59. }
  60. $param['site_id']=$this->siteId;
  61. $res = $reportService->getData($param);
  62. if(ACCOUNT_TYPE==3){
  63. return $res;
  64. }else{
  65. return json_decode($res['raw'],true);
  66. }
  67. }
  68. public function getSiteList(){
  69. // $reportService = new ReportService(API_URL, USERNAME,TOKEN,PASSWORD);
  70. $reportService = new ReportService($this->config);
  71. $ret = $reportService->getSiteList();
  72. if(ACCOUNT_TYPE==3){
  73. $siteList = $ret['list'];
  74. }else{
  75. $siteList = $ret['body']['data'][0]['list'];
  76. }
  77. $data=[];
  78. if($siteList){
  79. foreach ($siteList as $key => $value) {
  80. $data[]=[
  81. 'site_id'=>$value["site_id"],
  82. 'domain'=>$value["domain"]
  83. ];
  84. }
  85. }
  86. return $data;
  87. }
  88. /**
  89. * 获取网站今日流量与昨日对比
  90. */
  91. public function getOutline()
  92. {
  93. $param=[
  94. 'method' => 'overview/getOutline',
  95. ];
  96. return $this->getData($param);
  97. }
  98. /**
  99. * 获取今日趋势分析数据
  100. */
  101. public function trendTimeA()
  102. {
  103. $param=[
  104. 'method' => 'trend/time/a',
  105. 'metrics' => 'pv_count,visitor_count',//,ip_count,bounce_ratio,avg_visit_time
  106. 'start_date'=> date("Ymd"),
  107. 'end_date' => date("Ymd"),
  108. 'order' => 'simple_date_title,asc'
  109. ];
  110. $res=$this->getData($param);
  111. return $res;
  112. }
  113. /**
  114. * [getTimeTrendRpt 网站概况]
  115. * @return [type] [description]
  116. */
  117. public function getTimeTrendRpt($start_date,$end_date){
  118. $param=[
  119. 'method' => 'overview/getTimeTrendRpt',
  120. 'metrics' => 'pv_count,visitor_count',
  121. 'start_date' => $start_date,
  122. 'end_date' => $end_date,
  123. ];
  124. return $this->getData($param);
  125. }
  126. /**
  127. * 获取访问地域分布数据
  128. */
  129. public function getDistrictRpt($start_date,$end_date)
  130. {
  131. $param=[
  132. 'method'=>'overview/getDistrictRpt',
  133. 'metrics'=>'pv_count,visitor_count',
  134. 'start_date'=> $start_date,
  135. 'end_date' => $end_date,
  136. ];
  137. return $this->getData($param);
  138. }
  139. /**
  140. * 获取访问搜索词、来源网站、入口界面、受访页面、新老访客数据
  141. */
  142. public function getCommonTrackRpt($start_date,$end_date)
  143. {
  144. $param=[
  145. 'method'=>'overview/getCommonTrackRpt',
  146. 'metrics'=>'pv_count',
  147. 'start_date'=> $start_date,
  148. 'end_date' => $end_date,
  149. ];
  150. return $this->getData($param);
  151. }
  152. /**
  153. * 实时访客
  154. * @param [int] $page [页码]
  155. * @param [int] $max_results [每页数量]
  156. * @return [array] [description]
  157. */
  158. public function trendLatestA($page,$max_results){
  159. $start_index=($page-1)*$max_results;
  160. $param=[
  161. 'method' => 'trend/latest/a',
  162. 'metrics' => 'start_time,area,source,access_page,keyword,searchword,ip,visit_time,visit_pages',
  163. 'max_results' => $max_results,
  164. 'start_index' => $start_index
  165. ];
  166. return $this->getData($param);
  167. }
  168. /**
  169. * [sourceAll 全部来源]
  170. * @param [int] $page [页码]
  171. * @param [int] $max_results [每页数量]
  172. * @param [date] $start_date [开始时间]
  173. * @param [date] $end_date [结束时间]
  174. * @return [array] [description]
  175. */
  176. public function sourceAll($page,$max_results,$start_date,$end_date){
  177. $start_index=($page-1)*$max_results;
  178. $param=[
  179. 'method' => 'source/all/a',
  180. 'metrics' => 'pv_count,visitor_count,ip_count,bounce_ratio,avg_visit_time',
  181. 'viewType' => 'site',
  182. 'max_results' => $max_results,
  183. 'start_index' => $start_index,
  184. 'start_date' => $start_date,
  185. 'end_date' => $end_date,
  186. ];
  187. return $this->getData($param);
  188. }
  189. /**
  190. * [sourceSearchword 搜索词]
  191. * @param [int] $page [页码]
  192. * @param [int] $max_results [每页数量]
  193. * @param [date] $start_date [开始时间]
  194. * @param [date] $end_date [结束时间]
  195. * @return [array] [description]
  196. */
  197. public function sourceSearchword($page,$max_results,$start_date,$end_date){
  198. $start_index=($page-1)*$max_results;
  199. $param=[
  200. 'method' => 'source/searchword/a',
  201. 'metrics' => 'pv_count,visitor_count,ip_count,bounce_ratio,avg_visit_time',
  202. 'viewType' => 'site',
  203. 'max_results' => $max_results,
  204. 'start_index' => $start_index,
  205. 'start_date' => $start_date,
  206. 'end_date' => $end_date,
  207. ];
  208. return $this->getData($param);
  209. }
  210. /**
  211. * [visitToppage 受访页面]
  212. * @param [int] $page [页码]
  213. * @param [int] $max_results [每页数量]
  214. * @param [date] $start_date [开始时间]
  215. * @param [date] $end_date [结束时间]
  216. * @return [array] [description]
  217. */
  218. public function visitToppage($page,$max_results,$start_date,$end_date){
  219. $start_index=($page-1)*$max_results;
  220. $param=[
  221. 'method' => 'visit/toppage/a',
  222. 'metrics' => 'pv_count,visitor_count,ip_count,visit1_count,outward_count,exit_count,exit_ratio,average_stay_time',
  223. 'max_results' => $max_results,
  224. 'start_index' => $start_index,
  225. 'start_date' => $start_date,
  226. 'end_date' => $end_date,
  227. ];
  228. return $this->getData($param);
  229. }
  230. /**
  231. * [visitDistrict 地域分布(按省)]
  232. * @param [int] $page [页码]
  233. * @param [int] $max_results [每页数量]
  234. * @param [date] $start_date [开始时间]
  235. * @param [date] $end_date [结束时间]
  236. * @return [array] [description]
  237. */
  238. public function visitDistrict($param){
  239. $param['method']='visit/district/a';
  240. return $this->getData($param);
  241. }
  242. /**
  243. * [visitDistrictTop 地域分布(按城市)]
  244. * @param [type] $start_date [开始时间]
  245. * @param [type] $end_date [结束时间]
  246. * @param [type] $area [省份]
  247. * @return [type] [description]
  248. */
  249. public function visitDistrictTop($start_date,$end_date,$area){
  250. $param=[
  251. 'method' => 'visit/district/top',
  252. 'metrics' => 'pv_count,visitor_count,ip_count,bounce_ratio,avg_visit_time',
  253. 'start_date' => $start_date,
  254. 'end_date' => $end_date,
  255. 'viewType' => 'city',
  256. 'area' => $area
  257. ];
  258. return $this->getData($param);
  259. }
  260. /**
  261. * [visitWorld 地域分布(按国家)]
  262. * @param [int] $page [页码]
  263. * @param [int] $max_results [每页数量]
  264. * @param [date] $start_date [开始时间]
  265. * @param [date] $end_date [结束时间]
  266. * @return [array] [description]
  267. */
  268. public function visitWorld($start_date,$end_date){
  269. $param=[
  270. 'method' => 'visit/world/a',
  271. 'metrics' => 'pv_count,visitor_count,ip_count,bounce_ratio,avg_visit_time',
  272. 'start_date' => $start_date,
  273. 'end_date' => $end_date,
  274. ];
  275. return $this->getData($param);
  276. }
  277. public function rankGetData($start_date,$end_date){
  278. $param=[
  279. 'method' => 'opt/searchwordrank/getData',
  280. 'metrics' => '',
  281. 'start_date' => $start_date,
  282. 'end_date' => $end_date,
  283. 'viewType' => 'searchword_rank'
  284. ];
  285. return $this->getData($param);
  286. }
  287. public function rankGetWords(){
  288. $param=[
  289. 'method' => 'opt/searchwordrank/getWords',
  290. ];
  291. return $this->getData($param);
  292. }
  293. public function rankSetWords($words){
  294. $param=[
  295. 'method' => 'opt/searchwordrank/setWords',
  296. 'words' => $words
  297. ];
  298. return $this->getData($param);
  299. }
  300. public function reportSend($param){
  301. $param['method'] = 'home/reportsend/add';
  302. return $this->getData($param);
  303. }
  304. }