百度统计Tongji API可以为网站接入者提供便捷的获取网站流量数据的通道, 从而使网站接入者可向网站帐号提供数据分析、运营监控、网站推广等服务
Token申请:
登录百度统计商业账号:https://tongji.baidu.com/
在管理->其他设置->数据导出服务->开通数据导出服务
php相关代码配置:
$this->bdapi_config = [
'curl' => 'https://api.baidu.com/json/tongji/v1/ReportService/getSiteList', //站点列表获取
'dada_url' => 'https://api.baidu.com/json/tongji/v1/ReportService/getData', //数据库获取接口
'username' => 'xxxxxx',
'password' => 'xxxxxx',
'token' => 'xxxxxx',
'host' => ['xxx.com','www.xxx.com']
];
//获取商业号站点列表
protected function get_website(){
$siteid = Cache::connect($this->options)->get('site_id');
if (intval($siteid) > 0) return $siteid;
$params = [
'header' => [
"username" => $this->bdapi_config['username'],
"password" => $this->bdapi_config['password'],
"token" => $this->bdapi_config['token'],
"account_type" => 1
]
];
$result = $this->curl_post(json_encode($params),$this->bdapi_config['curl']);
$data_arr = [];
if ($result){
$data = isset($result['body']['data'][0]['list']) ? $result['body']['data'][0]['list'] : [];
if (!empty($data)){
foreach ($data as $key=>$vo){
if (in_array($vo['domain'],$this->bdapi_config['host'])){
Cache::connect($this->options)->set('site_id',$vo['site_id']);
return $data_arr['site_id'] = $vo['site_id'];
}
continue;
}
} else {
return $data_arr['error_msg'] = 0;
}
unset($result,$data);
}
return $data_arr['error_msg'] = 0;
}
//受访页面
public function visit_toppage(){
$info = input('param.');
$body = [
"site_id" => $info['siteid'],
"start_date" => "20200901",
"end_date" => "20200923",
"metrics" => "pv_count,visitor_count",
"method" => "visit/toppage/a",
"start_index" => $info['page'],//页码
"max_results" => 10 //条数
];
$data = $this->curl_post($this->prams_type($body));
if ($data){
$data_arr = [];
$list = $data['body']['data'][0]['result'];
$count = $list['total'];
//$timespan = $list['timeSpan'][0];
foreach ($list['items'][0] as $key=>$vo){
foreach ($list['items'][1] as $key2=>$vv){
$data_arr[$key]['id'] = ($key+1);
$data_arr[$key]['name'] = $vo[0]['name'];
$data_arr[$key2]['pv'] = $vv[0].'/次';
$data_arr[$key2]['uv'] = $vv[1].'/人';
}
}
unset($data,$list);
return [
'code' => 0,
'msg' => 'ok',
'count' => $count,
//'times' => $timespan,
'data' => $data_arr
];
}
}
protected function prams_type($body=[]){
$params = [
'header' => [
"username" => $this->bdapi_config['username'],
"password" => $this->bdapi_config['password'],
"token" => $this->bdapi_config['token'],
"account_type" => 1
],
'body' => $body
];
return json_encode($params);
}
protected function curl_post($params,$curl=null){
$curl = $curl ? $curl : $this->bdapi_config['dada_url'];
$result = curlPost($curl,$params);
if ($result){
return json_decode($result, true);
}
return $data_arr['error_msg'] = 0;
}
/**
* [提交数据]
* @param [type] $url [远程接口路径]
* @return [type]
*/
function curlPost($url, $postData) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
if ( ! curl_exec($ch))
{
$data = '';
}else{
$data = curl_multi_getcontent($ch);
}
curl_close($ch);
return $data;
}
前台页面显示代码:
使用lanyui组件:
<link rel="stylesheet" href="js/layui-v2.5.6/css/layui.css">
<script src="js/layui-v2.5.6/layui.all.js"></script>
<script>
layui.use(['table','laydate'], function(){
var table = layui.table,
laydate = layui.laydate;
//受访页面
table.render({
elem: '#visit-toppage'
,size: 'sm'
,height: 312
,url: 'xxxx?siteid={$siteid}&page=0'
,page: true
,cols: [[
{field: 'id', title: 'ID', width:60, fixed: 'left'}
,{field: 'name', title: '页面地址_'}
,{field: 'uv', title: '访客数_', width:100}
,{field: 'pv', title: '浏览页面PV总次数', width:200, sort: true}
]]
});
laydate.render({
elem: '#time-span'
,theme: '#393D49'
,type: 'datetime'
,range: '-'
,format: 'yyyy/M/d'
});
});
</script>