腾讯广告线索平台(广点通拉取线索信息)

前言:

本文章记录腾讯广告数据拉取,这个和飞鱼的接口基本上就是大同小异,原腾讯线索平台对接文档
使用方法:将在后台获取的密钥和token替换到相应的位置,按自己的需要修改插入方法即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
/**
* User: 牧马人
* DateTime: 2020-04-14 12:16
* Desc:广点通拉取线索信息
*/
namespace Home\Controller;
use Common\CommonEvent\HomeController;
use Common\CommonEvent\AjaxCommon;
header("Content-Type: text/html;charset=utf-8");
class GdtController extends HomeController
{
//广点通请求地址
private $host = 'https://leads.qq.com/api/mv1';
private $pull_route = '/leads/list';
//获取几分钟前的数据
private $minute = 5;
//第*页
private $page = 1;
//每页*获取条数据
private $page_size = 50;

/**
* User: 牧马人
* DateTime: 2019/11/7 9:28
* Desc:运行入口
*/
public function run(){
$this->azw();
}

/**
* User: 牧马人
* DateTime: 2020-04-14 9:29
* Desc:账号为azw的配置
*/
private function azw(){
//广点通token
$token = '**************';
//广点通secret
$secret = '**************';
//开始时间
$start_time = time()-60*$this->minute;
//结束时间
$end_time = time();
//第几页
$page = $this->page;
//每页显示多少数据
$page_size = $this->page_size;
$signature = $this->base64($token,$secret);
$output = $this->curl($page,$page_size,$start_time,$end_time,$signature);
if ($output['code']==0){
$this->insertdata($output['data']['list']);
}
}
/**
* User: 牧马人
* DateTime: 2020-04-14 9:29
* Desc:签名加密
* @param string $token
* @param string $secret
* @return string
*/
private function base64($token='',$secret=''){
$token = $token; //⼴告主线索平台token
$secret = $secret; //⼴告主线索平台⽣成的接⼝密钥
$timestamp = time(); //即当前的秒级时间戳
$signature = base64_encode($token . "," . $timestamp . "," .sha1($token.".".$timestamp.".".$secret));
return $signature;
}

/**
* User: 牧马人
* DateTime: 2020-04-14 9:30
* Desc:进行curl请求,并将获取到的数据进行数组转换
* @param $page
* @param $page_size
* @param $start_time
* @param $end_time
* @param $signature
* @return mixed
*/
private function curl($page,$page_size,$start_time,$end_time,$signature){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->host.$this->pull_route.
'?page='.$page.'&page_size='.$page_size.
'&start_time='.$start_time.'&end_time='.$end_time);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'X-Signature:'.$signature,
'Accept:application/json',
'Content-Type:application/json',
));
$output = curl_exec($ch);
curl_close($ch);
return json_decode($output,true);
}

/**
* User: 牧马人
* DateTime: 2020-04-14 9:30
* Desc:插入到数据库
* @param $data
*/
private function insertdata($data){
$ajaxModel = new AjaxCommon();
foreach ($data as $value){
//尝试获取城市id
$city = M('Cities')->where("name='".$value['tel_location']."' and level = 1")->getField('cityid');
if (!$city){
$province = 3;
$city = 978;
}else{
//如果获取到城市id,再请求获取到省份id
$province = M('Cities')->where("name='".$value['tel_location']."' and level = 1")->getField('parentid');
}
$array['province'] = $province; //省id
$array['city'] = $city; //市id
$array['name'] = $value['leads_name']; //客户名称
$array['mobilephone'] = $value['leads_tel'];//客户手机号码
$array['isBuried'] = 2; //提交方式
$array['furniture'] = 0; //所选的家具
$array['original_time'] = strtotime($value['leads_create_time']);
$res = $ajaxModel->tenderSave(array(),'add',0,array(),1,$array);
if ($res['state']!='success'){
continue;
}
//添加utm
if ($res['bid']&&$res['cid']){
$data['utm'] = $value['adgroup_name'];
$data['dateline'] = time();
$data['id'] = $res['cid'];
$data['idtype'] = 'zhaobiao';
$utmRet = M('Utm')->add($data);
if (!$utmRet){
continue;
}
}
}
}
}