ReportService.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace addons\baiduwebtongji\library;
  3. use addons\baiduwebtongji\library\DataApiConnection;
  4. /**
  5. * class ReportService
  6. */
  7. /**
  8. * ReportService
  9. */
  10. class ReportService {
  11. private $apiUrl;
  12. private $userName;
  13. private $token;
  14. private $ucid;
  15. private $password;
  16. /**
  17. * construct
  18. * @param string $apiUrl
  19. * @param string $userName
  20. * @param string $token
  21. * @param string $ucid
  22. * @param string $password
  23. */
  24. public function __construct($config) {
  25. define('UUID', $config['uuid']);
  26. $this->config=$config;
  27. if(ACCOUNT_TYPE==1 || ACCOUNT_TYPE==2){
  28. $config_url=$report_url="https://api.baidu.com/json/tongji/v1/ReportService";
  29. if(ACCOUNT_TYPE==1){
  30. $this->header=[
  31. 'username' => $config['username'],
  32. 'password' => $config['password'],
  33. 'token' => $config['token'],
  34. 'account_type' => ACCOUNT_TYPE,
  35. ];
  36. }elseif(ACCOUNT_TYPE==2){
  37. $this->header=[
  38. 'userName' => $config['username'],
  39. 'accessToken' => $config['token']
  40. ];
  41. }
  42. }elseif(ACCOUNT_TYPE==3){
  43. $config_url="https://openapi.baidu.com/rest/2.0/tongji/config/getSiteList?access_token=".$config['token'];
  44. $report_url="https://openapi.baidu.com/rest/2.0/tongji/report/getData?access_token=".$config['token'];
  45. }
  46. $this->config_url = $config_url;
  47. $this->report_url = $report_url;
  48. }
  49. /**
  50. * get site list
  51. * @return array
  52. */
  53. public function getSiteList() {
  54. if(ACCOUNT_TYPE==3){
  55. $res=json_decode(file_get_contents($this->config_url),true);
  56. if(isset($res['error_code'])){
  57. $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);
  58. set_addon_config("baiduwebtongji",['token'=>$tokenArr['access_token'],'refresh_token'=>$tokenArr['refresh_token']]);
  59. $res=json_decode(file_get_contents("https://openapi.baidu.com/rest/2.0/tongji/config/getSiteList?access_token=".$tokenArr['access_token']),true);
  60. }
  61. return $res;
  62. }else{
  63. $apiConnection = new DataApiConnection();
  64. $apiConnection->init($this->config_url . '/getSiteList');
  65. $apiConnectionData = array(
  66. 'header' => $this->header,
  67. 'body' => null,
  68. );
  69. $apiConnection->POST($apiConnectionData);
  70. return array(
  71. 'header' => $apiConnection->retHead,
  72. 'body' => $apiConnection->retBody,
  73. 'raw' => $apiConnection->retRaw,
  74. );
  75. }
  76. }
  77. /**
  78. * get data
  79. * @param array $parameters
  80. * @return array
  81. */
  82. public function getData($parameters) {
  83. if(ACCOUNT_TYPE==3){
  84. $str=[];
  85. foreach ($parameters as $key => $value) {
  86. $str[]=$key."=".$value;
  87. }
  88. $str=implode("&",$str);
  89. $url=$this->report_url."&".$str;
  90. $res=json_decode(file_get_contents($url),true);
  91. return $res;
  92. }else{
  93. $apiConnection = new DataApiConnection();
  94. $apiConnection->init($this->report_url . '/getData');
  95. $apiConnectionData = array(
  96. 'header' => $this->header,
  97. 'body' => $parameters,
  98. );
  99. $apiConnection->POST($apiConnectionData);
  100. return array(
  101. 'header' => $apiConnection->retHead,
  102. 'body' => $apiConnection->retBody,
  103. 'raw' => $apiConnection->retRaw,
  104. );
  105. }
  106. }
  107. }