关于百度统计 tongji API 的使用

通过百度统计获取网站的PV UV 各种数据 ;

首先你要有它的登录账号!

首先要开通这个功能哟! 原理 = 用户名+密码+token+UUID =>换取site_id =>site_id调用数据接口

下载上面的APIdemo 这里有几个坑 你要修改一下demo代码

所有涉及CURL请求的

public function POST($data) {
$this->genPostData($data); $curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $this->url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2); //demo 里面这里的数是1 你要换成2 不然会报错
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, $this->headers);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $this->postData);
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

里面有个地方有一个代码 LoginService.inc.php

$retData = gzdecode($doLogin->retData, strlen($doLogin->retData));

换成

$retData = gzinflate(substr($doLogin->retData,10,-8));

请求参数 详情

if (count($siteList) > 0) {
$siteId = $siteList[0]['site_id'];
// get report data of the first site
$ret = $reportService->getData(array(
'site_id' => $siteId, //站点ID
'method' => 'trend/time/a', //趋势分析报告
'start_date' => '20170328', //所查询数据的起始日期
'end_date' => '20170329', //所查询数据的结束日期
'metrics' => 'pv_count,visitor_count', //所查询指标为PV和UV
'max_results' => 0, //返回所有条数
'gran' => 'day', //按天粒度
)); // echo $ret['raw'] . PHP_EOL;
$all_info = json_decode($ret['raw'],true);
echo json_encode($all_info);

结果

{

"header": {

"desc": "success",

"failures": [],

"oprs": 1,

"succ": 1,

"oprtime": 0,

"quota": 1,

"rquota": 49936,

"status": 0

},

"body": {

"data": [

  {

    "result": {

      "total": 2,

      "items": [

        [

          [

            "2017/03/29"

          ],

          [

            "2017/03/28"

          ]

        ],

        [

          [

            5224,

            1153

          ],

          [

            6818,

            1462

          ]

        ],

        [],

        []

      ],

      "timeSpan": [

        "2017/03/28 - 2017/03/29"

      ],

      "sum": [

        [

          12042,

          2615

        ],

        []

      ],

      "offset": 0,

      "pageSum": [

        [

          12042,

          2615

        ],

        [],

        []

      ],

      "fields": [

        "simple_date_title",

        "pv_count",

        "visitor_count"

      ]

    }

  }

]

}

}

http://blog.163.com/zhuxun_why/blog/static/268139050201722961244335?ignoreua

你可能感兴趣的:(关于百度统计 tongji API 的使用)