分享一下消息訂閱推送
相信小伙伴們在做項目的時候都會有消息訂閱推送的功能,比方說做外賣項目或者是商城的時候,
用戶下單,需要給用戶微信推送一條訂閱消息
廢話不多說 直接上代碼
1.首先需要在微信小程序上面申請訂閱消息模板
然后按照詳細內(nèi)容在代碼里進行配置并調(diào)用
下面是我封裝的代碼:文章來源:http://www.zghlxwxcb.cn/news/detail-540697.html
/** 消息訂閱
* $appid:微信小程序appid、$appsecret:微信小程序秘鑰、$touser:用戶openid、$template_id:模板id
*/
public function sendMessage($appid,$appsecret,$touser,$template_id,$order_address,$order_type,$time,$order_status,$orer_remarks){
$page = '';//跳轉(zhuǎn)頁面不填則模板無跳轉(zhuǎn)。
$url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $appid . '&secret=' . $appsecret;
$res = json_decode(file_get_contents($url), true);
$access_token = $res['access_token'];
//請求url
$urls = 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=' . $access_token;
//發(fā)送內(nèi)容
$data = [];
//接收者(用戶)的 openid
$data['touser'] = $touser;
//所需下發(fā)的訂閱模板id
$data['template_id'] = $template_id;
//點擊模板卡片后的跳轉(zhuǎn)頁面,僅限本小程序內(nèi)的頁面。支持帶參數(shù),(示例index?foo=bar)。該字段不填則模板無跳轉(zhuǎn)。
$data['page'] = $page;
//模板內(nèi)容,格式形如 { "key1": { "value": any }, "key2": { "value": any } }
$data['data'] = [
"thing1" => [
'value' => $order_address
],
"thing2" => [
'value' => $order_type
],
"date3" => [
'value' => $time
],
'thing4' => [
'value' => $order_status
],
'thing9' => [
'value' => $orer_remarks
],
];
//跳轉(zhuǎn)小程序類型:developer為開發(fā)版;trial為體驗版;formal為正式版;默認為正式版
$data['miniprogram_state'] = '';
return self::curlPost($urls,json_encode($data));
}
//發(fā)送post請求
static function curlPost($url,$data)
{
$ch = curl_init();
$params[CURLOPT_URL] = $url; //請求url地址
$params[CURLOPT_HEADER] = FALSE; //是否返回響應(yīng)頭信息
$params[CURLOPT_SSL_VERIFYPEER] = false;
$params[CURLOPT_SSL_VERIFYHOST] = false;
$params[CURLOPT_RETURNTRANSFER] = true; //是否將結(jié)果返回
$params[CURLOPT_POST] = true;
$params[CURLOPT_POSTFIELDS] = $data;
curl_setopt_array($ch, $params); //傳入curl參數(shù)
$content = curl_exec($ch); //執(zhí)行
curl_close($ch); //關(guān)閉連接
return $content;
}
調(diào)用然后調(diào)用接口就OK了。
這里有一個問題,消息訂閱通知不能一直發(fā)送, 微信官方有個變態(tài)的規(guī)則,就是當(dāng)你一直發(fā)送消息訂閱通知,微信官方會認定這是垃圾消息推送,會給你拒絕接收消息。過一會就會好的。文章來源地址http://www.zghlxwxcb.cn/news/detail-540697.html
到了這里,關(guān)于PHP調(diào)用微信消息訂閱推送的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!