### [如何实现小程序推送模板消息](https://www.prosaas.cn/article/16653) **Published:** 2020-06-10T06:24:11 **Author:** 5201314 **Excerpt:** 如何实现小程序推送模板消息?以下为开发步骤获取用户的openid获取form_id或者prepay_id获取a… ![](https://img.php.cn/upload/article/000/000/049/5ee06bc1779e4859.jpg) **如何实现小程序推送模板消息?** 以下为开发步骤 **获取用户的openid** **获取form\_id或者prepay\_id** **获取access\_token** **发送模板消息** **DEMO下载地址** 重要提示 此方法为利用PHP内置curl模块发送请求,开发中都是以此方法访问微信服务器获取数据,其中url为接口地址,params为携带参数,ispost为请求方式,https为证书校验 public static function curl($url, $params = false, $ispost = 0, $https = 0) { $httpInfo = array(); $ch = curl\_init(); curl\_setopt($ch, CURLOPT\_HTTP\_VERSION, CURL\_HTTP\_VERSION\_1\_1); curl\_setopt( $ch, CURLOPT\_HTTPHEADER, array( 'Content-Type: application/json; charset=utf-8' ) ); curl\_setopt($ch, CURLOPT\_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36'); curl\_setopt($ch, CURLOPT\_CONNECTTIMEOUT, 30); curl\_setopt($ch, CURLOPT\_TIMEOUT, 30); curl\_setopt($ch, CURLOPT\_RETURNTRANSFER, true); if ($https) { curl\_setopt($ch, CURLOPT\_SSL\_VERIFYPEER, FALSE); // 对认证证书来源的检查 curl\_setopt($ch, CURLOPT\_SSL\_VERIFYHOST, FALSE); // 从证书中检查SSL加密算法是否存在 } if ($ispost) { curl\_setopt($ch, CURLOPT\_POST, true); curl\_setopt($ch, CURLOPT\_POSTFIELDS, $params); curl\_setopt($ch, CURLOPT\_URL, $url); } else { if ($params) { if (is\_array($params)) { $params = http\_build\_query($params); } curl\_setopt($ch, CURLOPT\_URL, $url . '?' . $params); } else { curl\_setopt($ch, CURLOPT\_URL, $url); } } $response = curl\_exec($ch); if ($response === FALSE) { return false; } $httpCode = curl\_getinfo($ch, CURLINFO\_HTTP\_CODE); $httpInfo = array\_merge($httpInfo, curl\_getinfo($ch)); curl\_close($ch); return $response; } 获取用户的openid 微信小程序代码,建议放在app.js全局保存,方便调用 wx.login({ success: function (res) { wx.request({ url: "www.xxx.com", //你的服务器接口地址 data: { code:res.code //通过wx.login获取code发送至服务器 }, header: { 'content-type': 'application/json' }, success: function (res) { that.globalData.OpenId=res.data.openid //存储openid } }) } }) 服务器端PHP代码,我用的是laravel框架,可自行重构 public function getUserInfo(Request $request) { $code = $request->get("code"); $appid=""; //小程序appid $secret=""; //小程序secret $Url = 'https://api.weixin.qq.com/sns/jscode2session?appid=' . $appid . '&secret=' . $secre . '&js\_code=' . $code . '&grant\_type=authorization\_code'; //微信官方给出的接口,利用小程序内获取的code置换openid $UserInfo=HttpUtils::curl($Url, $params = false, $ispost = 0, $https = 1); //上文给出的curl方法 echo $UserInfo; //输出结果,其中包含openid } 获取form\_id或者prepay\_id 本篇只做简要介绍,留到下篇博客微信支付讲解 1.form\_id为小程序内提交表单时所产生的id,当用户在小程序内发生过提交表单行为且该表单声明为要发模板消息的,开发者需要向用户提供服务时,可允许开发者向用户在7天内推送有限条数的模板消息(1次提交表单可下发1条,多次提交下发条数独立,相互不影响) 2.prepay\_id为小程序拉起微信支付时所产生的预支付id,当用户在小程序内完成过支付行为,可允许开发者向用户在7天内推送有限条数的模板消息(1次支付可下发3条,多次支付下发条数独立,互相不影响) 获取access\_token 此方法为获取access\_token为后续发送模板消息提供参数,我用的是laravel框架,可自行重构 public static function access\_token(){ $appid=""; //小程序appid $secret=""; //小程序secret $Url="https://api.weixin.qq.com/cgi-bin/token?grant\_type=client\_credential&appid=". $appid."&secret=".$secret; //微信给出的获取access\_token的接口 $access\_token=Cache::get("access\_token"); //查询缓存中是否已存在access\_token if($access\_token==""){ $access\_token=json\_decode(self::curl($Url))->{"access\_token"}; //访问接口获取access\_token Cache::put("access\_token",$access\_token,120); //设置缓存,过期时间2小时 } return $access\_token; } 发送模板消息 发送模板消息方法 public static function SendMsg($data,$access\_token){ $MsgUrl="https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access\_token=".$access\_token; //微信官方接口,需要拼接access\_token return json\_decode(self::curl($MsgUrl,$params=json\_encode($data),$ispost=1,$https=1)); //访问接口,返回参数 } 调用示例 public function test(Request $request){ $form\_id=$request->get("form\_id"); $openid=$request->get("openid"); $access\_token=WxUtils::access\_token(); $data=\[ "touser"=>$openid, //接收用户的openid "template\_id"=>"k03-Sk5c4eNlQKrS4VqI4cKjEil7JyvcouxtKBFkVcs", //模板id "page"=>"pages/index/index",//点击模板消息跳转至小程序的页面 "form\_id"=>$form\_id, //可为表单提交时form\_id,也可是支付产生的prepay\_id "data"=>\[ "keyword1"=>\[ "value"=> "五公司", //自定义参数 "color"=> '#173177'//自定义文字颜色 \], "keyword2"=>\[ "value"=> "保洁服务",//自定义参数 "color"=> '#173177'//自定义文字颜色 \], "keyword3"=>\[ "value"=> "2018年10月",//自定义参数 "color"=> '#173177'//自定义文字颜色 \], "keyword4"=>\[ "value"=> "已发布",//自定义参数 "color"=> '#173177'//自定义文字颜色 \], "keyword5"=>\[ "value"=> "请至小程序订单列表进行查看",//自定义参数 "color"=> '#173177'//自定义文字颜色 \], \] \]; $res=WxUtils::SendMsg($data,$access\_token); //返回结果 } > 总结 > > 1.openid获取挺简单的,就是你的appid和secret别搞错就行 > > 2.access\_token同上,也是别搞错填写的参数,严格按照官方给出的文档填 > > 3.模板消息的data中,跳转小程序的路由严格按照你小程序所写路由填写,跳转pages/index/index别写成…/index/inex 相关推荐:小程序教程 以上就是如何实现小程序推送模板消息的详细内容,更多请关注知企PROSAAS其它相关文章! **Tags:** cURL, https, JS, laravel, php, 微信小程序, 接口, 重构 **Categories:** 后端开发 ---