admin 管理员组文章数量: 1086866
上报下单行为 腾讯广告 微信 公众号 PHP
微信后台去推公众号关注后下单链路文档:
此网站使用TP
上报下单行为
第一步 获取相关配置
public $APPID= 'appid'; //公众号里获取
public $APPSECRET = 'appsecret'; //公众号里获取
public $TOKEN = 'token';//接口获取
public $USER_ACTION_SET_ID='数据源ID';//接口获取
第二步 获取数据源ID
- 方法类:Weixingg里的 addUserActionSets 是创建数据源。(代码在文章后边)
- 数据源创建一次即可 可以把创建好的数据源ID记做 $USER_ACTION_SET_ID
第三步 上报(调用写好的方法类-Weixingg)
- 获取click_id ,click_id是在广告页面的url上的gdt_vid的值 (此处上报不上传clickid)
- open_id 是微信用户的openid
- $total_price 是下单金额
$promotion_link='完整的广告url;$uinfo = UserModel::detail($userid);if($uinfo){$wmodel = new Weixingg();$wmodel->addUserActionsCom($promotion_link,$click_id,$total_price,$uinfo['open_id']);}
方法类Weixingg
<?php
namespace app\api\model;
use think\Cache;
use think\Log;class Weixingg
{public $APPID= 'appid';public $APPSECRET = 'appsecret';public $TOKEN = 'TOKEN_NAME';public $USER_ACTION_SET_ID='数据源id';/*** 公众号关注-Open MKT API* @param $urli* @param $click_id* @param $money* @throws \Exception*/public function addUserActionsCom($urli,$click_id,$money,$openid){$access_token = $this->getTokenFun();$url='?'.'version=v1.0&access_token='.$access_token;$params['actions'][0] = array('user_action_set_id' => $this->USER_ACTION_SET_ID,'url' =>$urli,'action_time' => time(),'action_type' =>'COMPLETE_ORDER','user_id' =>array('wechat_app_id' =>$this->APPID,'wechat_openid' =>$openid,),'action_param'=>array("value"=> $money,"source"=>'Biz',"claim_type"=>1,));$result = $this->curl_post_https($url,json_encode($params));$result = json_decode($result,true);if($result['errcode']!=0){Log::write('提交失败 重新提交!');}}/*** H5-Open MKT API* @param $urli* @param $click_id* @param $money* @throws \Exception*/public function addUserActions($urli,$click_id,$money){$access_token = $this->getTokenFun();$url='?'.'version=v1.0&access_token='.$access_token;$params['user_action_set_id'] = $this->USER_ACTION_SET_ID;$params['actions'][0] = array('url' =>$urli,'action_time' => time(),'action_type' =>'COMPLETE_ORDER','action_param'=>array("value"=> $money));if(!empty($click_id)){$params['actions'][0]['trace']['click_id'] = $click_id;}$result = $this->curl_post_https($url,json_encode($params));Log::write('addUserActions:'.print_r($result,true));//$result = json_decode($result,true);//print_r($result);}//创建数据源-微信公众号要使用type=WECHAT的数据源public function addUserActionSets(){$access_token = $this->getTokenFun();$url='/'.'user_action_sets/add?version=v1.0&access_token='.$access_token;$params['type'] = 'WECHAT';$params['name'] = '数据源名字';$params['description'] = '数据源简介 ';$params['wechat_app_id'] = $this->APPID;$result = $this->curl_post_https($url,json_encode($params));Log::write('addUserActionSets:'.print_r($result,true));//$result = json_decode($result,true);//print_r($result);}//创建数据源-h5的数据源public function addUserActionH5Sets(){$access_token = $this->getTokenFun();$url='/'.'user_action_sets/add?version=v1.0&access_token='.$access_token;$params['type'] = 'WEB';$params['name'] = '数据源名称';$params['description'] = '微信 数据源名称';$result = $this->curl_post_https($url,json_encode($params));Log::write('addUserActionSets:'.print_r($result,true));//$result = json_decode($result,true);//print_r($result);}//获取access_tokenpublic function getTokenFun(){$cacheKey = '@access_token';if (!Cache::get($cacheKey)) {Log::write('没有缓存cacheKey:');}else{$zhi = Cache::get($cacheKey);Log::write('缓存cacheKey:'.print_r($zhi,true));return $zhi;}$data = Cache::get($this->TOKEN);Log::write('expires_in:'.print_r($data['expires_in'],true));if($data && $data['expires_in']>time()){return $data['access_token'];}else{$url = '?'.'grant_type=client_credential&appid='.$this->APPID.'&secret='.$this->APPSECRET;$result= $this->curl_get_https($url);Log::write('getTokenFun:'.print_r($result,true));$result = json_decode($result,true);$result['expires_in'] = time()+7000;Log::write('expires_in2:'.print_r($result['expires_in'],true));Cache::set($this->TOKEN,$result);return $result['access_token'];}}//获取access_tokenpublic function getTokenFunNoCache(){$url = '?'.'grant_type=client_credential&appid='.$this->APPID.'&secret='.$this->APPSECRET;$result= $this->curl_get_https($url);Log::write('getTokenFunNoCache:'.print_r($result,true));$result = json_decode($result,true);$result['expires_in'] = time()+7000;Log::write('expires_inNoCache2:'.print_r($result['expires_in'],true));Cache::set($this->TOKEN,$result);return $result['access_token'];}public function curl_post_https($request_url,$parameters){$curl = curl_init();curl_setopt($curl, CURLOPT_URL, $request_url);curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 60);curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);curl_setopt($curl, CURLOPT_POST, 1);curl_setopt($curl, CURLOPT_POSTFIELDS, $parameters);curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type:application/json"));$response = curl_exec($curl);if (curl_error($curl)) {$error_msg = curl_error($curl);$error_no = curl_errno($curl);curl_close($curl);throw new \Exception($error_msg, $error_no);}curl_close($curl);return $response;}public function curl_get_https($url,$header=array()){ // 模拟提交数据函数$curl = curl_init(); // 启动一个CURL会话curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referercurl_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); // 执行操作if (curl_errno($curl)) {echo 'Errno'.curl_error($curl);//捕抓异常}curl_close($curl); // 关闭CURL会话return $tmpInfo; // 返回数据,json格式}public function valid(){$echoStr = $_GET['echostr'];if($this->checkSignature()){echo $echoStr; #坑点,看下面的常见坑介绍exit; #一定要停止php运行,避免产生不必要的字串符}}private function checkSignature(){$signature = $_GET["signature"];$timestamp = $_GET["timestamp"];$nonce = $_GET["nonce"];$token = 'huihui123456';$tmpArr = array($token, $timestamp, $nonce);sort($tmpArr, SORT_STRING);$tmpStr = implode( $tmpArr );$tmpStr = sha1( $tmpStr );if( $tmpStr == $signature ){return true;}else{return false;}}
}
?>
本文标签: 上报下单行为 腾讯广告 微信 公众号 PHP
版权声明:本文标题:上报下单行为 腾讯广告 微信 公众号 PHP 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/b/1688243787a196456.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论