先进入百度统计,数据管理-数据api页面开通。
![图片[1]-分享调用百度统计api-大鹏资源网-提供丰富的插件软件资源和详细教程,专注技术分享与学习!](https://www.01zt.com/wp-content/uploads/2024/08/5c2ff7b0bf15612af65b708757617967.webp)
可以参考Tongji API用户手册 · Tongji API用户手册 (baidu.com)
第一步:
通过如下URL进入百度账号登录页,此处的登录账号就是您登录百度统计查看报告数据的账号,登录完成后将跳转至获取code的页面:
http://openapi.baidu.com/oauth/2.0/authorize?response_type=code&client_id=
第二步:
用户同意授权后,将跳转至一个获取CODE
的页面。复制CODE
值后可将其加入以下URL换取ACCESS_TOKEN
:
http://openapi.baidu.com/oauth/2.0/token?grant_type=authorization_code&code=
浏览器访问返回如下:
{
“expires_in”: 2592000,
“refresh_token”:”2.385d55f8615fdfd9edb7c4b5ebdc3e39.604800.1293440400-2346678-124328″,
“access_token”:”1.a6b7dbd428f731035f771b8d15063f61.86400.1292922000-2346678-124328″,
“session_secret”:”ANXxSNjwQDugf8615OnqeikRMu2bKaXCdlLxn”,
“session_key”:” 248APxvxjCZ0VEC43EYrvxqaK4oZExMB”,
“scope”:”basic”
}
下面成品代码,可以在php页面baidutongji()调用:
function baidutongji() {
$baidutj_option = get_option('baidutj');
// 获取当前时间的 Unix 时间戳
$current_time = current_time('timestamp');
// 指定的时间点
$specified_time = $baidutj_option['refresh_access_time']; // 转换为 Unix 时间戳
// 计算10天的秒数
$ten_days_in_seconds = 10 * 24 * 60 * 60;
if(empty($baidutj_option)){
$baidutjarray = array(
'API_Key' => '{CLIENT_ID}填写您的API Key',
'Secret_Key' => '{CLIENT_SECRET}填写您的Secret Key',
'site_id' => 19380713,
'refresh_token' => "122.443afc0e24229207623e60e83c55b2f9.YnGRafREPAmM44Q6LwIFwYvriMV4OA7AAvoxK0x.GUFCww",
'access_token' => "121.3f3a399ab8481697a80a83e967220dcd.YBh-M6x1NLukSuvvaE4NmJtkC75A6pqvxhs82h5.6iTvuw",
'refresh_access_token' => "122.443afc0e24229207623e60e83c55b2f9.YnGRafREPAmM44Q6LwIFwYvriMV4OA7AAvoxK0x.GUFCww",
'refresh_access_time' => $current_time,
'tongji_refresh_time' => $current_time,
);
update_option('baidutj', $baidutjarray, '', 'no');
}
// 检查指定时间是否超过当前时间的10天 超过10天刷新access_token这样可以永久不失效
if ($current_time - $specified_time > $ten_days_in_seconds) {
$array_refresh = array(
"grant_type" => "refresh_token",
"refresh_token" => $baidutj_option['refresh_token'],
'client_id' => $baidutj_option['API_Key'],
'client_secret' => $baidutj_option['Secret_Key'],
);
$url_refresh_token = dp_httpRequest_function("http://openapi.baidu.com/oauth/2.0/token?",10,$array_refresh);
$data_refresh_token = json_decode($url_refresh_token, true);
if (!empty($data_refresh_token["refresh_token"])) {
$baidutj_refresh_token = array(
'refresh_token' => $data_refresh_token["refresh_token"],
'access_token' => $data_refresh_token["access_token"],
'refresh_access_token' => $data_refresh_token["refresh_token"],
'refresh_access_time' => $current_time,
);
$new_option_refresh = array_merge($baidutj_option, $baidutj_refresh_token);
wp_cache_delete($new_option_refresh, 'baidutj');
update_option('baidutj', $new_option_refresh);
}
}
//每小时 调用百度统计并更新数据库数据
if ($current_time - $baidutj_option['tongji_refresh_time'] > 3600 || empty($baidutj_option['tongji_sum'])) {
$array_baidutj = array(
"access_token" => $baidutj_option["access_token"],
"site_id" => $baidutj_option["site_id"],
'method' => "overview/getTimeTrendRpt",
'start_date' => "20230808",
'end_date' => current_time('Ymd'),
'metrics' => "pv_count,ip_count",
);
$url_baidutj = curl_function("https://openapi.baidu.com/rest/2.0/tongji/report/getData?",8,$array_baidutj);
$data_baidutj = json_decode($url_baidutj, true);
$statistics = $data_baidutj['result']['items']['1'];
$pv_count_sum = 0;
$Array_pv_count = [];
$Array_ip_count = [];
if (!empty($statistics)) {
foreach ($statistics as $value) {
if (is_numeric($value[0])) {
$pv_count_sum += $value['0'];
$Array_pv_count[] = $value['0'];
$Array_ip_count[] = $value['1'];
$today_pv_count = $value['0'];//今天pv
$today_ip_count = $value['1'];//今天ip
}
}
$yesterday_pv_count = $Array_pv_count[count($Array_pv_count) - 2];//昨天pv
$yesterday_ip_count = $Array_ip_count[count($Array_ip_count) - 2];//昨天ip
$array_tongji_sum = array(
"tongji_refresh_time"=>$current_time,
'tongji_sum' => array("today_pv_count"=>$today_pv_count,"today_ip_count"=>$today_ip_count,"yesterday_pv_count"=>$yesterday_pv_count,"yesterday_ip_count"=>$yesterday_ip_count,"pv_count_sum"=>$pv_count_sum),
);
$new_option_sum = array_merge($baidutj_option, $array_tongji_sum);
wp_cache_delete($new_option_sum, 'baidutj');
update_option('baidutj', $new_option_sum);
}
}
return '今日pv量 '.$baidutj_option['tongji_sum']['today_pv_count'].' 今日ip '.$baidutj_option['tongji_sum']['today_ip_count'].' 昨日pv量 '.$baidutj_option['tongji_sum']['yesterday_pv_count'].' 昨日ip '.$baidutj_option['tongji_sum']['yesterday_ip_count'].' 总访问量 '.$baidutj_option['tongji_sum']['pv_count_sum'].' (数据来源于百度统计)';
}
function curl_function($url, $time = 3, $data = "", $referer = "")
{
$header = array('User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36','Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3','Accept-Encoding: gzip, deflate',);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_ENCODING,'gzip');
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_MAXREDIRS,5);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $time); // 设置连接超时为10秒
curl_setopt($ch, CURLOPT_TIMEOUT, 10); // 设置执行超时为30秒
curl_setopt($ch, CURLOPT_FAILONERROR, false);
if (!empty($data)) {//判断是否为POST请求
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
if(!empty($referer)){curl_setopt($ch, CURLOPT_REFERER,$referer);}
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// 检查 HTTP 状态码是否为 200 或者响应是否为空
if ($httpCode == 200 && !empty($output)) {
return $output;
}
curl_close($ch);
return false;
}
从上述步骤得到的数据中包含ACCESS_TOKEN
和REFRESH_TOKEN
两个值,其中ACCESS_TOKEN
的有效期为一个月,REFRESH_TOKEN
的有效期为十年。REFRESH_TOKEN
的作用就是刷新获取新的ACCESS_TOKEN
和REFRESH_TOKEN
,如此反复操作来实现ACCESS_TOKEN
有效期永久的机制。如果出现突发情况,比如:服务器失联好多天,可以重新第一步第二步获取ACCESS_TOKEN
。
2本文地址 https://www.01zt.com/skill/wordpress/1824.html. 如若转载,请注明文章出处:大鹏博客资源网。
3本站内容观点不代表本站立场,并不代表本站赞同其观点和对其真实性负责。
4若作商业用途,请联系原作者授权,若本站侵犯了您的权益请在右侧悬浮联系站长进行删除处理。
5本站所有内容均来源于网络,仅供学习与参考,请勿商业运营,严禁从事违法、侵权等任何非法活动,否则后果自负.
暂无评论内容