php 小程序订阅消息

消息能力是小程序能力中的重要组成,我们为开发者提供了订阅消息能力,以便实现服务的闭环和更优的体验。

订阅消息推送位置:服务通知

订阅消息下发条件:用户自主订阅

订阅消息卡片跳转能力:点击查看详情可跳转至该小程序的页面

使用说明 步骤一:获取模板 ID

在微信公众平台手动配置获取模板 ID:
登录 https://mp.weixin.qq.com 获取模板,如果没有合适的模板,可以申请添加新模板,审核通过后可使用。

php 小程序订阅消息

步骤二:获取下发权限

前端的接口:我们可以预设多个模板ID一起申请,用户勾选的会返回accept,也是就通过,后端就可以记录了。

wx.requestSubscribeMessage({ tmplIds: [\'\'], success (res) { } })

php 小程序订阅消息

步骤三:调用接口下发订阅消息

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

/ * 发送订阅消息 * @return [type] [description] */ public function sendSubMessage($openId = "openId",$tmpl = "TEMPLATE_ID",array $data,$page=\'\',$miniprogram_state=\'formal\'){ $params[\'touser\'] = $openId; $params[\'template_id\'] = $tmpl; $params[\'page\'] = $page; $params[\'miniprogram_state\'] = $miniprogram_state; $params[\'data\'] = $data; $url = \'https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=\'.$token; $re = $this->curl_url($url,$params); return $re; } private function curl_url($url, $json) { $body = json_encode($json); $headers = array("Content-type: application/json;charset=UTF-8", "Accept: application/json", "Cache-Control: no-cache", "Pragma: no-cache"); $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $body); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); $result = curl_exec($ch); curl_close($ch); return $result; }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zwgppy.html