600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > PHP实现模板消息推送(微信小程序版)

PHP实现模板消息推送(微信小程序版)

时间:2024-05-14 02:40:01

相关推荐

PHP实现模板消息推送(微信小程序版)

以下为开发步骤

获取用户的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: "", //你的服务器接口地址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./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./cgi-bin/token?grant_type=client_credential&appid=". $appid."&secret=".$secret; //微信给出的获取access_token的接口$access_token=Cache::get("access_token"); //查询缓存中是否已存在access_tokenif($access_token==""){$access_token=json_decode(self::curl($Url))->{"access_token"}; //访问接口获取access_tokenCache::put("access_token",$access_token,120); //设置缓存,过期时间2小时}return $access_token;}

发送模板消息

发送模板消息方法

public static function SendMsg($data,$access_token){$MsgUrl="https://api./cgi-bin/message/wxopen/template/send?access_token=".$access_token; //微信官方接口,需要拼接access_tokenreturn 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"=> "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

4.作者自己写了个小程序BadBoy,有兴趣的可以搜索来玩玩,其中的代码可找作者索取

5.微信小程序交流群:895964328 php交流群:165728481

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。