概述
前端实现
获取微信广告端传来的click_id,并传给后端,需要注意的是如果是自己的落地页那就要看看click_id是否是5位,若是5位则需要让创建微信广告的人员改创建方式,即用户从朋友圈点击广告进来后不直接跳自己写的落地页,创建时加一层内置的,否则上报失败。(click_id 格式:18 位或 16 位长度字符串)
<script>
function GetQueryString(name)
{
var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
var r = window.location.search.substr(1).match(reg);//search,查询?后面的参数,并匹配正则
if(r!=null)return
unescape(r[2]); return null;
}
//ul = window.location.href;
clickid = GetQueryString("gdt_vid");
</script>
后端实现
以model类为例,上报失败的原因大多在于上报的数据格式有问题(第4步),虽返回值正常但是上报后没效果,以下为实现
1)创建数据源
public function createSorce(){
$access_token = $this->getAccessToken();
$url = "https://api.weixin.qq.com/marketing/user_action_sets/add?version=v1.0&access_token=$access_token";
$header=array(
//"Accept: application/json",
"Content-Type: application/json;charset=utf-8",
);
$post_data = '{
"type": "WEB",
"name": "wxadtest",
"description": "test"
}';
$res = json_decode($this->curl_post($url, $post_data,$header));
return $res;
}
2)数据源报表查询
public function uasReport(){
$access_token = $this->getAccessToken();
$user_action_set_id = 'xxxx';
$date = date("Y-m-d");
$url = "https://api.weixin.qq.com/marketing/user_action_set_reports/get?version=v1.0&access_token=$access_token&user_action_set_id=$user_action_set_id&time_granularity=HOURLY&aggregation=RESERVATION";
$date_range = '{"start_date":'.'"'.$date.'"'.',"end_date":'.'"'.$date.'"'.'}';
$url = $url . "&" ."date_range=" . urlencode($date_range);
$res = json_decode($this->httpGet($url));
return $res;
}
3)数据源上报
//数据源查询
public function searchUas(){
$user_act_set_id = 'xxxx';
$access_token = $this->getAccessToken();
$url = "https://api.weixin.qq.com/marketing/user_action_sets/get?version=v1.0&access_token=$access_token&user_action_set_id=1108137102";
$res = json_decode($this->httpGet($url));
return $res;
}
4)数据源上报(特别注意的是在上报方法中$post_data的数据中的拼接)
public function upMp($click_id){
$user_act_set_id = 'xxxx';
$access_token = $this->getAccessToken();
$timestamp = time();
$ui = "xxxx";
$url = "https://api.weixin.qq.com/marketing/user_actions/add?version=v1.0&access_token=$access_token";
$header=array(
//"Accept: application/json",
"Content-Type: application/json;charset=utf-8",
);
$post_data = '{
"actions":[
{
"user_action_set_id":'.'"'.$user_act_set_id.'"'.',
"url":'.'"'.$ui.'"'.',
"action_time":'.'"'.$timestamp.'"'.',
"action_type":"RESERVATION",
"trace":{
"click_id":'.'"'.$click_id.'"'.'
}
}
]
}';
$res = json_decode($this->curl_post($url, $post_data,$header));
$errcode = $res->errcode;
return $errcode;
}
其他:
//post
private function curl_post($url, $post_data,$header){
$curl = curl_init();
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);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS,$post_data);
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER,$header);
$res = curl_exec($curl);
curl_close($curl);
return $res;
}
//accessToken
private function getAccessToken() {
// access_token 应该全局存储与更新,以下代码以写入到文件中做示例
$data = json_decode($this->get_php_file("access_token.php"));
if ($data->expire_time < time()) {
// 如果是企业号用以下URL获取access_token
//$url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$this->appId&corpsecret=$this->appSecret";
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->appId&secret=$this->appSecret";
$res = json_decode($this->httpGet($url));
$access_token = $res->access_token;
if ($access_token) {
$data->expire_time = time() + 7000;
$data->access_token = $access_token;
$this->set_php_file("access_token.php", json_encode($data));
}
} else {
$access_token = $data->access_token;
}
return $access_token;
}
最后
以上就是魁梧热狗为你收集整理的微信广告数据源创建查询与上报-PHP的全部内容,希望文章能够帮你解决微信广告数据源创建查询与上报-PHP所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复