這期內容當中小編將會給大家帶來有關使用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 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;
php是一個嵌套的縮寫名稱,是英文超級文本預處理語言,它的語法混合了C、Java、Perl以及php自創新的語法,主要用來做網站開發,許多小型網站都用php開發,因為php是開源的,從而使得php經久不衰。
上述就是小編為大家分享的使用PHP怎么實現小程序批量通知推送了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。