laravel 接入百度统计接口

  因为业务上需要统计网站统计量,又不想自己开发,所以就使用百度统计获取访客量,网站是使用laravel5.7框架,在百度统计开发平台生成一个token;

1、现在env配置百度统计

#百度统计配置
BAIDUTJ_API=https://api.baidu.com/json/tongji/v1/ReportService/getData
BAIDUTJ_ACCOUNT_TYPE=
BAIDUTJ_SITEID=
BAIDUTJ_USERNAME=
BAIDUTJ_PASSWORD=
BAIDUTJ_TOKEN=

2、在config目录下新建文件baidutj.php

<?php
/**
 * Created by PhpStorm.
 * User: dongge
 * Date: 19-1-5
 * Time: 下午3:22
 */
return [
    'url' => env('BAIDUTJ_API', null),
    'type' => env('BAIDUTJ_ACCOUNT_TYPE', null),
    'siteId' => env('BAIDUTJ_SITEID', null),
    'username' => env('BAIDUTJ_USERNAME', 'null'),
    'password' => env('BAIDUTJ_PASSWORD', null),
    'token' => env('BAIDUTJ_TOKEN', null),
];

3、在app\Service下新建一个文件BaiduTjService.php

<?php
/**
 * Created by PhpStorm.
 * User: dongge
 * Date: 19-1-5
 * Time: 下午3:10
 */

namespace App\Service;

class BaiduTjService
{
    /**
     * @param $url
     * @param $headersArr
     * @param $bodyArr
     * @return bool|mixed
     */
    public static function getData($url, $headersArr, $bodyArr)
    {
        if (!isset($headersArr['type'], $headersArr['username'], $headersArr['password'], $headersArr['token'])) {
            return false;
        }
        if (!isset($bodyArr['siteId'], $bodyArr['method'], $bodyArr['s_time'], $bodyArr['e_time'])) {
            return false;
        }
        if (!isset($bodyArr['metrics'], $bodyArr['gran'], $bodyArr['max_results'])) {
            return false;
        }
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 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_POST, 1);
        curl_setopt($curl, CURLOPT_HTTPHEADER, [
            'Content-Type: application/json' // 防止无法接收CURLOPT_POSTFIELDS内容
        ]);
        curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode([
            'header' => [
                'account_type' => $headersArr['type'],
                'username' => $headersArr['username'],
                'password' => $headersArr['password'],
                'token' => $headersArr['token'],
            ],
            'body' => [
                'siteId' => $bodyArr['siteId'],
                'method' => $bodyArr['method'],
                'start_date' => $bodyArr['s_time'],
                'end_date' => $bodyArr['e_time'],
                'metrics' => $bodyArr['metrics'],
                'gran' => $bodyArr['gran'],
                'max_results' => $bodyArr['max_results']
            ]
        ]));
        curl_setopt($curl, CURLOPT_TIMEOUT, 30);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $result = curl_exec($curl);
        if (curl_errno($curl)) {
            return false;
        }
        curl_close($curl);

        return json_decode($result, true);
    }
}

4、调用service获取访客数据:

        /** 访客 */
        $url = config('baidutj.url');
        $headers['type'] = config('baidutj.type');
        $headers['username'] = config('baidutj.username');
        $headers['password'] = config('baidutj.password');
        $headers['token'] = config('baidutj.token');
        $body['siteId'] = config('baidutj.siteId');
        $body['method'] = 'trend/time/a';
        $body['s_time'] = date('Y-m-d', $start);
        $body['e_time'] = date('Y-m-d', $end);
        $body['metrics'] = 'visitor_count';
        $body['gran'] = 'month';
        $body['max_results'] = 0;
        $vData = BaiduTjService::getData($url, $headers, $body);
        $visitor = 0;
        if ($vData && $vData['header']['succ'] ==1) {
            $visitor = $vData['body']['data'][0]['result']['sum'][0][0];
        }

接口参数请查看 百度统计api接口文档

转载于:https://www.cnblogs.com/Dong-Ge/articles/10224989.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值