這篇文章主要介紹怎么實(shí)現(xiàn)小程序推送模板消息,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

如何實(shí)現(xiàn)小程序推送模板消息?
以下為開發(fā)步驟
獲取用戶的openid
獲取form_id或者prepay_id
獲取access_token
發(fā)送模板消息
DEMO下載地址
重要提示
此方法為利用PHP內(nèi)置curl模塊發(fā)送請(qǐng)求,開發(fā)中都是以此方法訪問微信服務(wù)器獲取數(shù)據(jù),其中url為接口地址,params為攜帶參數(shù),ispost為請(qǐng)求方式,https為證書校驗(yàn)
public static function curl($url, $params = false, $ispost = 0, $https = 0)
{
$httpInfo = array();
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt( $ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json; charset=utf-8'
)
);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36');
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if ($https) {
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // 對(duì)認(rèn)證證書來(lái)源的檢查
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); // 從證書中檢查SSL加密算法是否存在
}
if ($ispost) {
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_URL, $url);
} else {
if ($params) {
if (is_array($params)) {
$params = http_build_query($params);
}
curl_setopt($ch, CURLOPT_URL, $url . '?' . $params);
} else {
curl_setopt($ch, CURLOPT_URL, $url);
}
}
$response = curl_exec($ch);
if ($response === FALSE) {
return false;
}
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$httpInfo = array_merge($httpInfo, curl_getinfo($ch));
curl_close($ch);
return $response;
}獲取用戶的openid
微信小程序代碼,建議放在app.js全局保存,方便調(diào)用
wx.login({
success: function (res) {
wx.request({
url: "www.xxx.com", //你的服務(wù)器接口地址
data: {
code:res.code //通過wx.login獲取code發(fā)送至服務(wù)器
},
header: {
'content-type': 'application/json'
},
success: function (res) {
that.globalData.OpenId=res.data.openid //存儲(chǔ)openid
}
})
}
})服務(wù)器端PHP代碼,我用的是laravel框架,可自行重構(gòu)
public function getUserInfo(Request $request)
{
$code = $request->get("code");
$appid=""; //小程序appid
$secret=""; //小程序secret
$Url = '/tupian/20230522/jscode2session
$UserInfo=HttpUtils::curl($Url, $params = false, $ispost = 0, $https = 1); //上文給出的curl方法
echo $UserInfo; //輸出結(jié)果,其中包含openid
}獲取form_id或者prepay_id
本篇只做簡(jiǎn)要介紹,留到下篇博客微信支付講解
1.form_id為小程序內(nèi)提交表單時(shí)所產(chǎn)生的id,當(dāng)用戶在小程序內(nèi)發(fā)生過提交表單行為且該表單聲明為要發(fā)模板消息的,開發(fā)者需要向用戶提供服務(wù)時(shí),可允許開發(fā)者向用戶在7天內(nèi)推送有限條數(shù)的模板消息(1次提交表單可下發(fā)1條,多次提交下發(fā)條數(shù)獨(dú)立,相互不影響)
2.prepay_id為小程序拉起微信支付時(shí)所產(chǎn)生的預(yù)支付id,當(dāng)用戶在小程序內(nèi)完成過支付行為,可允許開發(fā)者向用戶在7天內(nèi)推送有限條數(shù)的模板消息(1次支付可下發(fā)3條,多次支付下發(fā)條數(shù)獨(dú)立,互相不影響)
獲取access_token
此方法為獲取access_token為后續(xù)發(fā)送模板消息提供參數(shù),我用的是laravel框架,可自行重構(gòu)
public static function access_token(){
$appid=""; //小程序appid
$secret=""; //小程序secret
$Url="/tupian/20230522/token
$access_token=Cache::get("access_token"); //查詢緩存中是否已存在access_token
if($access_token==""){
$access_token=json_decode(self::curl($Url))->{"access_token"}; //訪問接口獲取access_token
Cache::put("access_token",$access_token,120); //設(shè)置緩存,過期時(shí)間2小時(shí)
}
return $access_token;
}發(fā)送模板消息
發(fā)送模板消息方法
public static function SendMsg($data,$access_token){
$MsgUrl="/tupian/20230522/send
return json_decode(self::curl($MsgUrl,$params=json_encode($data),$ispost=1,$https=1)); //訪問接口,返回參數(shù)
}調(diào)用示例
public function test(Request $request){
$form_id=$request->get("form_id");
$openid=$request->get("openid");
$access_token=WxUtils::access_token();
$data=[
"touser"=>$openid, //接收用戶的openid
"template_id"=>"k03-Sk5c4eNlQKrS4VqI4cKjEil7JyvcouxtKBFkVcs", //模板id
"page"=>"pages/index/index",//點(diǎn)擊模板消息跳轉(zhuǎn)至小程序的頁(yè)面
"form_id"=>$form_id, //可為表單提交時(shí)form_id,也可是支付產(chǎn)生的prepay_id
"data"=>[
"keyword1"=>[
"value"=> "五公司", //自定義參數(shù)
"color"=> '#173177'//自定義文字顏色
],
"keyword2"=>[
"value"=> "保潔服務(wù)",//自定義參數(shù)
"color"=> '#173177'//自定義文字顏色
],
"keyword3"=>[
"value"=> "2018年10月",//自定義參數(shù)
"color"=> '#173177'//自定義文字顏色
],
"keyword4"=>[
"value"=> "已發(fā)布",//自定義參數(shù)
"color"=> '#173177'//自定義文字顏色
],
"keyword5"=>[
"value"=> "請(qǐng)至小程序訂單列表進(jìn)行查看",//自定義參數(shù)
"color"=> '#173177'//自定義文字顏色
],
]
];
$res=WxUtils::SendMsg($data,$access_token); //返回結(jié)果
}總結(jié)
1.openid獲取挺簡(jiǎn)單的,就是你的appid和secret別搞錯(cuò)就行
2.access_token同上,也是別搞錯(cuò)填寫的參數(shù),嚴(yán)格按照官方給出的文檔填
3.模板消息的data中,跳轉(zhuǎn)小程序的路由嚴(yán)格按照你小程序所寫路由填寫,跳轉(zhuǎn)pages/index/index別寫成…/index/inex
以上是“怎么實(shí)現(xiàn)小程序推送模板消息”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
網(wǎng)站欄目:怎么實(shí)現(xiàn)小程序推送模板消息-創(chuàng)新互聯(lián)
網(wǎng)站鏈接:http://chinadenli.net/article28/gopcp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈、軟件開發(fā)、響應(yīng)式網(wǎng)站、網(wǎng)站制作、關(guān)鍵詞優(yōu)化、品牌網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容