佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > PHP實現(xiàn)小程序批量通知推送

PHP實現(xiàn)小程序批量通知推送

熱門標簽:科大訊飛語音識別系統(tǒng) 團購網(wǎng)站 銀行業(yè)務 Mysql連接數(shù)設置 Linux服務器 服務器配置 電子圍欄 阿里云

本文實例為大家分享了PHP實現(xiàn)小程序批量通知推送的具體代碼,供大家參考,具體內(nèi)容如下

基本效果如下:

具體實現(xiàn)如下:

1.配置模板

2.從小程序獲取formId,傳到后臺存到表里

下發(fā)條件說明

1).支付

當用戶在小程序內(nèi)完成過支付行為,可允許開發(fā)者向用戶在7天內(nèi)推送有限條數(shù)的模板消息(1次支付可下發(fā)3條,多次支付下發(fā)條數(shù)獨立,互相不影響)

2).提交表單

當用戶在小程序內(nèi)發(fā)生過提交表單行為且該表單聲明為要發(fā)模板消息的,開發(fā)者需要向用戶提供服務時,可允許開發(fā)者向用戶在7天內(nèi)推送有限條數(shù)的模板消息(1次提交表單可下發(fā)1條,多次提交下發(fā)條數(shù)獨立,相互不影響)

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后臺實現(xiàn)推送

一共使用兩個提供的api

1).獲取小程序 access_token

請求地址

GET https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credentialappid=APPIDsecret=APPSECRET

2).發(fā)送模板消息

請求地址

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

php完整代碼:

//需要修改的字段
//1.小程序AppId,小程序secret,去微信公眾平臺找
//2.表名,時間字段
//3.模板ID,去我的模板里找
//4.跳轉(zhuǎn)地址,你知道的
$url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credentialappid=小程序AppIdsecret=小程序secret';
   $info = file_get_contents($url);
   $json = json_decode($info);/*對json數(shù)據(jù)解碼*/
   $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;$icount($row);$i++){
  $openid = $row[$i]['openid'];
  $formid = $row[$i]['formId'];
  $id = $row[$i]['id'];
  $post_data = array(
   "touser"=> $openid,
   "template_id"=> "模板ID",
   "page"=> "跳轉(zhuǎn)地址",
   "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;

文檔:地址

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • php實現(xiàn)微信公眾號主動推送消息
  • 微信小程序 消息推送php服務器驗證實例詳解
  • 解析php做推送服務端實現(xiàn)ios消息推送
  • php ios推送(代碼)
  • php實現(xiàn)微信模板消息推送
  • PHP快速推送微信模板消息
  • php實現(xiàn)websocket實時消息推送
  • php redis實現(xiàn)對200w用戶的即時推送
  • php實現(xiàn)socket推送技術的示例

標簽:棗莊 江蘇 廣元 衢州 大理 衡水 蚌埠 萍鄉(xiāng)

巨人網(wǎng)絡通訊聲明:本文標題《PHP實現(xiàn)小程序批量通知推送》,本文關鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權(quán)與本站無關。
  • 相關文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    钦州市| 安吉县| 乐至县| 海淀区| 隆尧县| 阳信县| 安达市| 公主岭市| 西青区| 三穗县| 汤原县| 黑水县| 吐鲁番市| 思茅市| 青田县| 晋州市| 普陀区| 揭东县| 博乐市| 宁城县| 商河县| 克山县| 东辽县| 长海县| 绥滨县| 葫芦岛市| 鸡东县| 介休市| 原平市| 陇南市| 开平市| 浮梁县| 东山县| 墨脱县| 婺源县| 积石山| 江川县| 霍林郭勒市| 邢台市| 永嘉县| 三明市|