模板消息概述
模板消息用于公众号向用户发送重要的服务通知等。
必要条件
发送模板消息需要满足一些条件,具体要求请仔细阅读公众号官方手册 :
https://developers.weixin.qq.com/doc/offiaccount/Message_Management/Template_Message_Interface.html
实现步骤
1、登录微信公众号如果左侧没有模板消息按钮点击添加功能按钮添加模板消息。
2、点击“从模板库中添加”按钮选择合适的模板并添加(如果没有合适的点击“帮我们完善模板库”提交自己的模板)。
3、开发消息发送功能。
发送消息代码
<?php
class indexController extends grace{
public $graceWeChat;
public function __construct(){
parent::__construct();
$this->graceWeChat = new \phpGrace\tools\graceWeChat();
}
public function sendTemplateMsg(){
$msg = '{
"touser":"op15jsy7D14d8BAvDTTLTdumuz_I",
"template_id":"HPdb1gj1omKqj6ITUNtP85OeAs01wh0pQc5-QZutums",
"url":"http://www.phpgrace.com",
"topcolor":"#FF0000",
"data":{
"first": {
"value":"您的快递已经领取",
"color":"#173177"
},
"keyword1":{
"value":"'.mt_rand(111111, 999999).'",
"color":"#173177"
},
"keyword2": {
"value":"2017-07-07",
"color":"#173177"
},
"remark":{
"value":"如不是本人操作请联系客服",
"color":"#173177"
}
}
}';
$res = $this->graceWeChat->sendTemplateMsg($msg);
p($res);
}
}