微信小程序> 微信广告数据源创建查询与上报-PHP

微信广告数据源创建查询与上报-PHP

浏览量:3011 时间: 来源:Mr.php

前端实现

获取微信广告端传来的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;    }

其他:

//postprivate 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;    }//accessTokenprivate 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;    }

 

版权声明

即速应用倡导尊重与保护知识产权。如发现本站文章存在版权问题,烦请提供版权疑问、身份证明、版权证明、联系方式等发邮件至197452366@qq.com ,我们将及时处理。本站文章仅作分享交流用途,作者观点不等同于即速应用观点。用户与作者的任何交易与本站无关,请知悉。

产品经理

手机 : 13312967497

擅长 : 小程序流量变现

扫码领取礼包

最新资讯

热门模板

  • 头条
  • 搜狐
  • 微博
  • 百家
  • 一点资讯
  • 知乎