小程序发送公众号模板消息

2021/8/25 11:06:42

本文主要是介绍小程序发送公众号模板消息,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

前置条件: 1.小程序和公众号必须关联  微信公众平台上进入设置 进行关联 https://mp.weixin.qq.com/ 2.需要在微信开放平台注册账号(注册费用三百元) 然后进行关联操作 https://open.weixin.qq.com 3.公众号模板消息   整体思路: 用户授权openid->换取unionID(前端获取) 小程序内部引导用户关注公众号(official-account组件) //前端实现 公众号批量获取关注用户的openid(https://api.weixin.qq.com/cgi-bin/user/get?access_token=ACCESS_TOKEN|) 拿公众号的openid换取UnionID  https://api.weixin.qq.com/cgi-bin/user/info?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN 两个unionid相同的 就是同一个用户   具体实现:   1.小程序获取unionid和openid                
    public function openid(){
            $code=$request->input('code'); //前端调用wx.login方法拿到的code
            $appid = 'XXXXXXXX';
            $secret = 'XXXXXXXX';
            $url = 'https://api.weixin.qq.com/sns/jscode2session?appid=' . $appid . '&secret=' . $secret . '&js_code=' . $code . '&grant_type=authorization_code';
            $res = $this->getJson($url); //如果不报错 结果应该为session_key openid unionid
            $openid=$res['openid'];
            $unionid=$res['unionid'];
        }
        function getJson($url){
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            $output = curl_exec($ch);
            curl_close($ch);
            return json_decode($output, true);
        }

 

  2.获取关注公众号的用户列表  文档地址:https://developers.weixin.qq.com/doc/offiaccount/User_Management/Getting_a_User_List.html #获取关注公众号的openid列表           
  public function openid(){
       $access_token = $this->returnAssKey();
       $NEXT_OPENID="oD5bqvy5elflcaJZIfrow8t9v6uU"; //该参数是从那个openid开始获取之后的所有openid 如果不填 默认从第一个开始获取
   
       $url="https://api.weixin.qq.com/cgi-bin/user/get?access_token=$access_token&next_openid=$NEXT_OPENID";
       
       $ass_key = $this->getJson($url); //如果没有报错 会返回三个参数  
       $openids=$ass_key['data']['openid'];  //这就是关注公众号的用户列表
      
      
    }
    #获取access_token
    public function returnAsskey()
    {
        $appid="XXXXXX";
        $secret="XXXXXX";  //公众号的appid和secret
        $url     = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$secret";
        $ass_key = $this->curlGet($url);
        $a1 = $ass_key->access_token;
        return $a1;
    }
    #请求链接地址
    public function curlGet($url)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $data = curl_exec($curl);
        $err  = curl_error($curl);
        curl_close($curl);
        return json_decode($data); //对数据进行json解码
    }

 

  3.获取某个公众号的openid所对应的unionid  文档地址:https://developers.weixin.qq.com/doc/offiaccount/User_Management/Get_users_basic_information_UnionID.html#UinonId 此处我用的是一对一接口,即一个openid换取一个unionid,文档中还有个批量用openid换取unionid的接口,按需求选择     
  #获取unionid
    public function unionid(){
        
            $openid="XXXXX";  //刚才获取到的openid列表中 要换取的某个openid
            $access_token = $this->returnAssKey();
            $url="https://api.weixin.qq.com/cgi-bin/user/info?access_token=$access_token&openid=$openid&lang=zh_CN";
            $ass_key = $this->getJson($url); //如果没有报错 回返回一大堆数据 我们只需要其中的unionid 其他看需要即可
            $unionid=$ass_key['unionid'];   //最终获取到的unionid
           
    }
    #获取access_token
    public function returnAsskey()
    {
        $appid="XXXXXX";
        $secret="XXXXXX";  //公众号的appid和secret
        $url     = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$secret";
        $ass_key = $this->curlGet($url);
        $a1 = $ass_key->access_token;
        return $a1;
    }
    #请求链接地址
    public function curlGet($url)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $data = curl_exec($curl);
        $err  = curl_error($curl);
        curl_close($curl);
        return json_decode($data); //对数据进行json解码
    }
    #请求链接地址
    function getJson($url){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($ch);
        curl_close($ch);
        return json_decode($output, true);
    }

 

4.发送公众号模板消息     
public function temMsg($order,$name,$phone)
    {
            $openid=“XXXXXX"; //公众号的openid
            $temid = 'vyCW15G-d-2PeNcUzz_kgod_ksNPSwjSKDOGYdvbljE'; //模板id
            $access_token = $this->returnAssKey();
            $url= 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=' . $access_token;
            $time=date('Y-m-d H:i',time());
        
            $data = array( //这里一定要按照微信给的格式
                "touser"      => "$openid", 
                "template_id" => $temid,
                "data"        => array(
                    "first"    => array(
                        "value" => '您有一个新订单,请及时确认!',
                        "color" => "#173177",
                    ),
                    "keyword1" => array(
                        "value" =>"$order",
                        "color" => "#173177",
                    ),
                    "keyword3" => array(
                        "value" => "$name",
                        "color" => "#173177",
                    ),
                    "keyword4" => array(
                        "value" => "$phone",
                        "color" => "#173177",
                    ),
                    'remark'   => array('value' => "下单时间:$time,请及时到后台根据处理订单!",
                        'color'=> '#FF0000',
                    ),
                ),
            );
 
 
        $res = $this->postCurl($url, $data, 'json'); //将data数组转换为json数据
        if ($res) {
            return ['state' => '0', 'msg' => $res];
        } else {
            return ['state' => '1', 'msg' => '系统错误!'];
        }
    }
    #获取access_token
    public function returnAsskey()
    {
        $appid="XXXXXX";
        $secret="XXXXXX";  //公众号的appid和secret
        $url     = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$secret";
        $ass_key = $this->curlGet($url);
        $a1 = $ass_key->access_token;
        return $a1;
    }
 
        #请求链接 get方式
    public function curlGet($url)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $data = curl_exec($curl);
        $err  = curl_error($curl);
        curl_close($curl);
        return json_decode($data); //对数据进行json解码
    }
 
    #获取链接数据 以post方式
    public function postCurl($url, $data, $type)
    {
        if ($type == 'json') {
            $data   = json_encode($data); //对数组进行json编码
            $header = array("Content-type: application/json;charset=UTF-8", "Accept: application/json", "Cache-Control: no-cache", "Pragma: no-cache");
        }
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        if (!empty($data)) {
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
        $res = curl_exec($curl);
        if (curl_errno($curl)) {
            echo 'Error+' . curl_error($curl);
        }
        curl_close($curl);
        return $res;
    }

 



这篇关于小程序发送公众号模板消息的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程