api思路:
(1)用户点击腾讯推广的广告,同时生成随机的 click_id,并追加到落地页的url。
(2)落地页的表单提交后,可以利用 API 将 click_id 和 转化类型(ActionType) 一并上报给腾讯社交广告
目的:经过腾讯社交广告计算,将最终数据呈现在投放端(e.qq.com)
开发流程:
准备步骤
public function text(){
$backurl="http://回调的域名/Admin/Gdt/back";//应用里面回调的域名
$result = urlencode($backurl);
$url = 'https://developers.e.qq.com/oauth/authorize?client_id=你的client_id&redirect_uri='.$result.'&state=&scope=';
header("Location:".$url);
}
获取access_token 和 refresh_token,存在时间限制
public function back(){
//判断是否含有code值
$txapi = M('txapi');
$data = $txapi->find();
if(empty($data)){
$action = 1;//获得新的 access_token 和 refresh_token
}else{
$time = time();
$edittime = strtotime($data['t_Datetime']);//编辑时间
$gaptime = $time-$edittime;
if($gaptime >= 86400 && $gaptime < 2592000){//24小时外
$action = 0; //仅仅获得新的access_token
}elseif($gaptime >= 2592000){//1个月外
$action = 1;//获得新的 access_token和refresh_token
}else{
$action = 2;//不变
}
}
$backurl="http://回调的域名/Admin/Gdt/back";
$result = urlencode($backurl);
if($action == 0){
//获取token值
$url ="https://api.e.qq.com/oauth/token?client_id=你的client_id&client_secret=你的client_secret&grant_type=refresh_token&refresh_token=".$data['t_Refresh_Token'];
$curl = curl_init(); // 启动一个CURL会话
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
$tmpInfo = curl_exec($curl); //返回api的json对象
curl_close($curl); //关闭URL请求
$dataRet=json_decode($tmpInfo,true);
$info['t_Access_Token'] = $dataRet['data']['access_token'];
$info['t_Refresh_Token'] = $dataRet['data']['refresh_token'];
$info['t_User_Action_Set_Id'] = '';
$info['t_Datetime'] = date("Y-m-d H:i:s");
$info['t_Id'] = $data['t_Id'];
$result = $txapi->save($info);
$this->getsource();
echo "编辑ok";
}elseif ($action == 1){
//获取token值
$authorization_code =$_GET['authorization_code'];
$url ="https://api.e.qq.com/oauth/token?client_id=你的client_id&client_secret=你的client_secret&grant_type=authorization_code&authorization_code=".$authorization_code."&redirect_uri=".$result;
$curl = curl_init(); // 启动一个CURL会话
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
$tmpInfo = curl_exec($curl); //返回api的json对象
//关闭URL请求
curl_close($curl);
$dataRet=json_decode($tmpInfo,true);
$info['t_Authorization_Code'] = $authorization_code;
$info['t_Access_Token'] = $dataRet['data']['access_token'];
$info['t_Refresh_Token'] = $dataRet['data']['refresh_token'];
$info['t_User_Action_Set_Id'] = '';
$info['t_Datetime'] = date("Y-m-d H:i:s");
$result = $txapi->add($info);
$this->getsource();
echo "添加ok";
}else{
//不变
$this->getsource();
echo "不变ok";
}
}
创建用户行为数据源
public function setsource(){
$txapi = M('txapi');
$data = $txapi->find();
$timestamp = time();
$nonce = rand();
$url = "https://api.e.qq.com/v1.0/user_action_sets/add?access_token=".$data['t_Access_Token']."×tamp=".$timestamp."&nonce=".$nonce;
$curl = curl_init(); // 启动一个CURL会话
$header=array(
"Accept: application/json",
"Content-Type: application/json",
);
//设置post数据
$post_data = array(
"account_id" => "你的account_id",
"type" => "WEB",
"name" => "测试哈哈",
"description" => "胡胡是个小可爱"
);
curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); // 从证书中检查SSL加密算法是否存在
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($post_data)); // Post提交的数据包
curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
curl_setopt($curl, CURLOPT_HTTPHEADER,$header);
$tmpInfo = curl_exec($curl); // 执行操作
curl_close($curl); // 关闭CURL会话
$dataRet=json_decode($tmpInfo,true);
}
获取用户行为数据源
public function getsource(){
$txapi = M('txapi');
$data = $txapi->find();
$timestamp = time();
$nonce = rand();
$url ="https://api.e.qq.com/v1.0/user_action_sets/get?access_token=".$data['t_Access_Token'].
"×tamp=".$timestamp."&account_id=你的account_id&nonce=".$nonce;
$curl = curl_init(); // 启动一个CURL会话
$header=array(
"Accept: application/json",
"Content-Type: application/json;charset=utf-8",
);
curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); // 从证书中检查SSL加密算法是否存在
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
curl_setopt($curl, CURLOPT_POSTFIELDS); // Post提交的数据包
curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
curl_setopt($curl, CURLOPT_HTTPHEADER,$header);
$tmpInfo = curl_exec($curl); // 执行操作
curl_close($curl); // 关闭CURL会话
$dataRet=json_decode($tmpInfo,true);
$reback['t_User_Action_Set_Id'] = $dataRet['data']['list'][0]['user_action_set_id'];
$reback['t_Id'] = $data['t_Id'];
$txapi->save($reback);
}
上传用户行为数据
public function useraction(){
$txapi = M('txapi');
$data = $txapi->find();
$timestamp = time();
$nonce = rand();
$url = "https://api.e.qq.com/v1.0/user_actions/add?timestamp=".$timestamp."&access_token=".$data['t_Access_Token']."&nonce=".$nonce;
$curl = curl_init(); // 启动一个CURL会话
$header=array(
//"Accept: application/json",
"Content-Type: application/json",
);
$post_data ='
{
"account_id":你的account_id,
"user_action_set_id":你的user_action_set_id,
"actions":
[
{
"outer_action_id": "",
"action_time":'.$timestamp.',
"user_id":{
"hash_imei":"f9efca36a3c30e1cf28170d86ecbf5e9"
},
"trace":{
"click_id":"1"
},
"action_type":"RESERVATION"
}
]
}
';
curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); // 从证书中检查SSL加密算法是否存在
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data); // Post提交的数据包
curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
curl_setopt($curl, CURLOPT_HTTPHEADER,$header);
$tmpInfo = curl_exec($curl); // 执行操作
curl_close($curl); // 关闭CURL会话
$dataRet=json_decode($tmpInfo,true);
var_dump($dataRet);exit;
}