欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

PHP实现小程序批量通知推送

程序员文章站 2023-11-05 13:05:34
本文实例为大家分享了php实现小程序批量通知推送的具体代码,供大家参考,具体内容如下 基本效果如下: 具体实现如下: 1.配置模板 2.从小程序获取fo...

本文实例为大家分享了php实现小程序批量通知推送的具体代码,供大家参考,具体内容如下

基本效果如下:

PHP实现小程序批量通知推送

PHP实现小程序批量通知推送

具体实现如下:

1.配置模板

PHP实现小程序批量通知推送

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

PHP实现小程序批量通知推送

3.php后台实现推送

一共使用两个提供的api

1).获取小程序 access_token

请求地址

get https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=appid&secret=appsecret

2).发送模板消息

请求地址

post https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=access_token

php完整代码:

//需要修改的字段
//1.小程序appid,小程序secret,去微信公众平台找
//2.表名,时间字段
//3.模板id,去我的模板里找
//4.跳转地址,你知道的
$url = 'https://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(
    'http' => array(
     'method' => 'post',
     'header' => 'content-type:application/json',/*header 需要设置为 json*/
     'content' => $post_data,
     'timeout' => 60/*超时时间*/
    )
   );
   $context = stream_context_create( $options );
   $result = file_get_contents( $url, false, $context );
   return $result;
 };
 
 $sql = "select * from 表名 where date_sub(curdate(), interval 6 day) <= date(时间字段) and used=0 group by openid";
 $rs = $pdo->query($sql);
 $row = $rs->fetchall();
 $resultsarr = array();
 $updatearr = array();
 $successnum = 0;
 $title = $_get['title'];
 $time = date('y年m月d日 h:i',time());
 for($i=0;$i<count($row);$i++){
  $openid = $row[$i]['openid'];
  $formid = $row[$i]['formid'];
  $id = $row[$i]['id'];
  $post_data = array(
   "touser"=> $openid,
   "template_id"=> "模板id",
   "page"=> "跳转地址",
   "form_id"=> $formid,
   "data"=> array(
    "keyword1"=> array(
       "value"=> $title
     ),
     "keyword2"=> array(
       "value"=> $time
     ),
     "keyword3"=> array(
       "value"=> "戳我进入涂呀首页查看"
     )),
   // "emphasis_keyword"=> "keyword1.data"  设置大字,自己试试就知道了
  ); 
  $post_data = json_encode($post_data);
  $postresults = send_post('https://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;

文档:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。