以下为开发步骤
获取用户的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
相关推荐:小程序教程
以上就是如何实现小程序推送模板消息的详细内容。
