您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

使用php实现微信小程序发送模板消息(附代码)

2024/2/17 14:53:20发布30次查看
本篇文章给大家带来的内容是关于使用php实现微信小程序发送模板消息(附代码),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。
本章将会简单说一下微信小程序的模板消息发送,相对来说比较简单,但也有一个小坑要注意的。
微信的地址为:
https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=access_token
相关参数为
参数必填说明
touser 是 接收者(用户)的 openid
template_id 是 所需下发的模板消息的id
page 否 点击模板卡片后的跳转页面,仅限本小程序内的页面。支持带参数,(示例index?foo=bar)。该字段不填则模板无跳转。
form_id 是 表单提交场景下,为 submit 事件带上的 formid;支付场景下,为本次支付的 prepay_id
data 是 模板内容,不填则下发空模板
color 否 模板内容字体的颜色,不填默认黑色 【废弃】
emphasis_keyword 否 模板需要放大的关键词,不填则默认无放大
基本参数和地址就是以上测试的,有的人会疑惑form_id,openid,tmeplate_id该从哪里获取下面为先简单说下
form_id可以由前端提供,前端可以在每一个按钮上面提交表单获取form_id给后端,同时后端拿到相关openid。在这里为建议的是前端尽可能多的给后端提供,就是每个按钮都提供给后端一个id,form_id的有效期为7天,不管运营人员有没有使用,数量多好过数量少。
template_id为模板id,可以登陆微信小程序后台在模板消息那里获取到。
当我们知道这些参数后,开发者肯定想马上测试一下,然后就让前端提供form_id,这里为将说下2个坑
第一坑:电脑获取的form_id是不可以使用的
第二坑:form_id是要手机真机获取的,但真机的同时还要是线上的项目,未上线本地测试会提示校验form_id,微信是没有说明这个注意点的,即是需要在审核成功发布后在线上测试。
接下来就说下后端代码
<?php //获取accesstoken public function getaccesstoken($appid,secret){ $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$secret}"; $res = $this->curl_get($url); $res = json_decode($res,1); return $res['access_token']; }//获取模板消息内容主体//因为是测试所以写死,大家可以通过传参的方式获取 public function getmsg($openid,$template_id,$form_id,$emphasis_keyword='keyword1'){ $data['data']= ['keyword1'=>['value'=>'test1','color'=>''],'keyword2'=>['value'=>'test2','color'=>''],'keyword3'=>['value'=>'test1','color'=>'']];//内容主体 $data['touser'] = $openid;//用户的openid $data['template_id'] = $template_id;//从微信后台获取的模板id $data['form_id'] = $form_id;//前端提供给后端的form_id $data['page'] = 'pages/index/index';//小程序跳转页面 $data['emphasis_keyword'] = $emphasis_keyword;//选择放大的字体 return $data; } public function send($appid,secret,$openid,$template_id,$form_id){ $access_token = $this->getaccesstoken($appid,secret); $send_url = 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send? access_token=' . $access_token; $data = $this->getmsg($openid,$template_id,$form_id); $str = $this->curl_post($send_url,json_encode($data)); $str = json_decode($str,1); return $str; } public function curl_post($url, $fields, $data_type='text') { $cl = curl_init(); if(stripos($url, 'https://') !== false) { curl_setopt($cl, curlopt_ssl_verifypeer, false); curl_setopt($cl, curlopt_ssl_verifyhost, false); curl_setopt($cl, curlopt_sslversion, 1); } curl_setopt($cl, curlopt_url, $url); curl_setopt($cl, curlopt_returntransfer, 1 ); curl_setopt($cl, curlopt_post, true); curl_setopt($cl, curlopt_postfields, $fields); $content = curl_exec($cl); $status = curl_getinfo($cl); curl_close($cl); if (isset($status['http_code']) && $status['http_code'] == 200) { if ($data_type == 'json') { $content = json_decode($content); } return $content; } else { return false; } } public function curl_get($url, $data_type='text') { $cl = curl_init(); if(stripos($url, 'https://') !== false) { curl_setopt($cl, curlopt_ssl_verifypeer, false); curl_setopt($cl, curlopt_ssl_verifyhost, false); curl_setopt($cl, curlopt_sslversion, 1); } curl_setopt($cl, curlopt_url, $url); curl_setopt($cl, curlopt_returntransfer, 1 ); $content = curl_exec($cl); $status = curl_getinfo($cl); curl_close($cl); if (isset($status['http_code']) && $status['http_code'] == 200) { if ($data_type == 'json') { $content = json_decode($content); } return $content; } else { return false; } } punblic function index(){ $appid = 'xxx';//小程序appid $openid = 'xxx';//接收用户的openid $template_id = 'xxx';//从微信后台获取的模板id $form_id = 'xxx';//七天内的formid $data = $this->send($appid,secret,$openid,$template_id,$form_id); var_dump($data);//打印测试结果 }
以上就是发送模板消息的代码,其实只要获取到几个相应的参数就可以 注意相关坑就可以成功测试发送了
相关推荐:
小程序中如何实现三级选择器组件?(代码示例)
微信小程序实例:微信小程序中弹窗的实现代码
微信小程序中如何进行页面的跳转
以上就是使用php实现微信小程序发送模板消息(附代码)的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product