如何用PHP编写代码实现小程序的批量消息推送功能?
- 内容介绍
- 文章标签
- 相关推荐
本文共计739个文字,预计阅读时间需要3分钟。
原文示例:本文字例为大国分享了PHP实现小程序批量通知推送的具体代码,供大家参考,具体内容如下:基本效果如下:+ 基本实现+ 基本效果+ 基本实现
1.配置模板
2.从小程序获取formId,传到后台存到表中
简化后:
示例代码展示PHP实现小程序批量通知功能,供参考。功能概述:+ 实现方法+ 效果展示+ 实现步骤:1.配置模板
2.获取formId并传至后台存储
本文实例为大家分享了PHP实现小程序批量通知推送的具体代码,供大家参考,具体内容如下
基本效果如下:
具体实现如下:
1.配置模板
2.从小程序获取formId,传到后台存到表里
下发条件说明
1).支付
当用户在小程序内完成过支付行为,可允许开发者向用户在7天内推送有限条数的模板消息(1次支付可下发3条,多次支付下发条数独立,互相不影响)
2).提交表单
当用户在小程序内发生过提交表单行为且该表单声明为要发模板消息的,开发者需要向用户提供服务时,可允许开发者向用户在7天内推送有限条数的模板消息(1次提交表单可下发1条,多次提交下发条数独立,相互不影响)
wxml
<form bindsubmit="getFormId" report-submit="true"> <button formType='submit'>获取formId</button> </form>
js
getFormId:function(e){ let formId = e.detail.formId; //得到formId,将formId传到后台存储到表里 }
我的表是这么建的:
createTime用来判断是否超过七天
used用来判断是否使用过这个formId
3.PHP后台实现推送
一共使用两个提供的api
1).获取小程序 access_token
请求地址
GET api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
2).发送模板消息
请求地址
POST api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=ACCESS_TOKEN
php完整代码:
//需要修改的字段 //1.小程序AppId,小程序secret,去微信公众平台找 //2.表名,时间字段 //3.模板ID,去我的模板里找 //4.跳转地址,你知道的 $url = 'api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=小程序AppId&secret=小程序secret'; $info = file_get_contents($url); $json = json_decode($info);/*对json数据解码*/ $arr = get_object_vars($json); $access_token = $arr['access_token']; function send_post( $url, $post_data ) { $options = array( 'api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token='.$access_token, $post_data); $res = json_decode($postResults); $res = get_object_vars($res); $errcode = $res['errcode']; if($errcode==0){ $sql1 = "update formIdList set used=1 where id=$id"; $pdo->exec($sql1); $successNum+=1; }; array_push($resultsArr, array('errcode'=>$errcode)); }; $Results = array( 'code'=>1, 'Results'=>array( 'successNum'=>$successNum, 'resultsArr'=>$resultsArr ), 'msg'=>'' ); $Results = json_encode($Results); echo $Results;
文档:地址
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。
本文共计739个文字,预计阅读时间需要3分钟。
原文示例:本文字例为大国分享了PHP实现小程序批量通知推送的具体代码,供大家参考,具体内容如下:基本效果如下:+ 基本实现+ 基本效果+ 基本实现
1.配置模板
2.从小程序获取formId,传到后台存到表中
简化后:
示例代码展示PHP实现小程序批量通知功能,供参考。功能概述:+ 实现方法+ 效果展示+ 实现步骤:1.配置模板
2.获取formId并传至后台存储
本文实例为大家分享了PHP实现小程序批量通知推送的具体代码,供大家参考,具体内容如下
基本效果如下:
具体实现如下:
1.配置模板
2.从小程序获取formId,传到后台存到表里
下发条件说明
1).支付
当用户在小程序内完成过支付行为,可允许开发者向用户在7天内推送有限条数的模板消息(1次支付可下发3条,多次支付下发条数独立,互相不影响)
2).提交表单
当用户在小程序内发生过提交表单行为且该表单声明为要发模板消息的,开发者需要向用户提供服务时,可允许开发者向用户在7天内推送有限条数的模板消息(1次提交表单可下发1条,多次提交下发条数独立,相互不影响)
wxml
<form bindsubmit="getFormId" report-submit="true"> <button formType='submit'>获取formId</button> </form>
js
getFormId:function(e){ let formId = e.detail.formId; //得到formId,将formId传到后台存储到表里 }
我的表是这么建的:
createTime用来判断是否超过七天
used用来判断是否使用过这个formId
3.PHP后台实现推送
一共使用两个提供的api
1).获取小程序 access_token
请求地址
GET api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
2).发送模板消息
请求地址
POST api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=ACCESS_TOKEN
php完整代码:
//需要修改的字段 //1.小程序AppId,小程序secret,去微信公众平台找 //2.表名,时间字段 //3.模板ID,去我的模板里找 //4.跳转地址,你知道的 $url = 'api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=小程序AppId&secret=小程序secret'; $info = file_get_contents($url); $json = json_decode($info);/*对json数据解码*/ $arr = get_object_vars($json); $access_token = $arr['access_token']; function send_post( $url, $post_data ) { $options = array( 'api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token='.$access_token, $post_data); $res = json_decode($postResults); $res = get_object_vars($res); $errcode = $res['errcode']; if($errcode==0){ $sql1 = "update formIdList set used=1 where id=$id"; $pdo->exec($sql1); $successNum+=1; }; array_push($resultsArr, array('errcode'=>$errcode)); }; $Results = array( 'code'=>1, 'Results'=>array( 'successNum'=>$successNum, 'resultsArr'=>$resultsArr ), 'msg'=>'' ); $Results = json_encode($Results); echo $Results;
文档:地址
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。

