当前位置:网站首页>微信公众号发送模板消息

微信公众号发送模板消息

2022-07-06 17:31:00 小Hier

第一步获取token

//公众号获取 token
     public  function getGzhAccessToken_wx($appId,$appSecret){
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appId}&secret={$appSecret}";
        $html = file_get_contents($url);
        $output = json_decode($html,true);
        $access_token = $output['access_token'];
        
        return $access_token;
    }

第二步获取所有关注的用户openid列表,存入数据表

//获取所有用户openid
    public function getUserList($token){
        $url="https://api.weixin.qq.com/cgi-bin/user/get?access_token={$token}&next_openid=";
        $html = file_get_contents($url);
        $data = json_decode($html,true);
        return $data;
    }

第三步通过openid获取用户基本信息

//获取用户基本信息
    public function getUserInfo($token,$openid){
        $url="https://api.weixin.qq.com/cgi-bin/user/info?access_token={$token}&openid={$openid}&lang=zh_CN";
        $html = file_get_contents($url);
        $data = json_decode($html,true);
        return $data;
    }

第四步发送模板消息

   public function sendUser($token){
        $data=[
            "touser"=>'oXhpA1UWjrvMost8kdAuiHIZUomA', //对方的openid,前一步获取
            "template_id"=>"Gphar4zkxFMSvdOJ-Hx6UvbnCaM_viZiBUIZTcH5tRU", //模板id
            // "miniprogram"=>["appid"=>"wxd35c1649beb11bba", //跳转小程序appid
            //     "pagepath"=>"pages/index/nindex/main"
            // ],//跳转小程序页面
             "data"=>[
               "first"=>[
                   "value"=> "测试信息", //自定义参数
                   "color"=> '#173177'//自定义颜色
               ],
               "keyword1"=>[
                   "value"=>'车美佳', //自定义参数
                   "color"=> '#173177'//自定义颜色
               ],
               "keyword2"=>[
                   "value"=> date('Y-m-d H:i:s'), //自定义参数
                   "color"=> '#173177'//自定义颜色
               ],
               "remark"=>[
                  "value"=> "如有变动,请主动联系用户", //自定义参数
                  "color"=> '#173177'//自定义颜色
               ],
            ]
      ];

      $gzh_url="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$token;
      $res=json_decode($this->curl_post_wx($gzh_url,json_encode($data)),true);
      return $res;
    }
  /**
    * @param $url
    * @param array $data
    * @return mixed
    * curl请求
    */
    public function curl_post_wx($url , $data=array()){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
        $tmpInfo = curl_exec($ch);
        if (curl_errno($ch)) {
            return false;
        }else{
            return $tmpInfo;
        }
    }

发送成功如下图

 

原网站

版权声明
本文为[小Hier]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_41965172/article/details/125614517