您的位置:首页 > 移动开发 > 微信开发

微信公众平台模板消息发送接口文档

2016-10-25 22:57 411 查看

为了保证用户不受到骚扰,在开发者出现需要主动提醒、通知用户时,才允许开发者在公众平台网站中模板消息库中选择模板,选择后获得模板ID,再根据模板ID向用户主动推送提醒、通知消息。模板消息调用时主要需要模板ID和模板中各参数的赋值内容。请注意:1.模板中参数内容必须以".DATA"结尾,否则视为保留字;2.模板保留符号"{{ }}"案例:{{first.DATA}}买家名字:{{keyword1.DATA}}付款金额:{{keyword2.DATA}}下单时间:{{keyword3.DATA}}{{remark.DATA}}具体调用方法第一步:获取模板ID通过在模板消息功能的模板库中使用需要的模板,可以获得模板ID。第二步:请求接口请注意,URL置空,则在发送后,点击模板消息会进入一个空白页面(ios),或无法点击(android)。POST请求https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN请求包为一个json:
{
"touser":"OPENID",
"template_id":"ngqIpbwh8bUfcSsECmogfXcV14J0tQlEpBO27izEYtY",
"url":"http://weixin.qq.com/download",
"topcolor":"#FF0000",
"data":{
"User": {
"value":"黄先生",
"color":"#173177"
},
"Date":{
"value":"06月07日 19时24分",
"color":"#173177"
},
"CardNumber":{
"value":"0426",
"color":"#173177"
},
"Type":{
"value":"消费",
"color":"#173177"
},
"Money":{
"value":"人民币260.00元",
"color":"#173177"
},
"DeadTime":{
"value":"06月07日19时24分",
"color":"#173177"
},
"Left":{
"value":"6504.09",
"color":"#173177"
}
}
}
效果图:事件推送在模版消息发送任务完成后,微信服务器会将是否送达成功作为通知,发送到开发者中心中填写的服务器配置地址中。1、送达成功时,推送的XML如下:2、送达由于用户拒收(用户设置拒绝接收公众号消息)而失败时,推送的XML如下:3、送达由于其他原因失败时,推送的XML如下:返回码说明在调用模板消息接口后,会返回JSON数据包。正常时的返回JSON数据包示例:{"errcode":0,"errmsg":"ok","msgid":200228332}错误时的返回JSON数据,形式类似,错误码请见本页下方返回码说明。返回码 说明-1 系统繁忙0 请求成功40001 验证失败40002 不合法的凭证类型40003 不合法的OpenID40004 不合法的媒体文件类型40005 不合法的文件类型40006 不合法的文件大小40007 不合法的媒体文件id40008 不合法的消息类型40009 不合法的图片文件大小40010 不合法的语音文件大小40011 不合法的视频文件大小40012 不合法的缩略图文件大小40013 不合法的APPID41001 缺少access_token参数41002 缺少appid参数41003 缺少refresh_token参数41004 缺少secret参数41005 缺少多媒体文件数据41006 access_token超时42001 需要GET请求43002 需要POST请求43003 需要HTTPS请求44001 多媒体文件为空44002 POST的数据包为空44003 图文消息内容为空45001 多媒体文件大小超过限制45002 消息内容超过限制45003 标题字段超过限制45004 描述字段超过限制45005 链接字段超过限制45006 图片链接字段超过限制45007 语音播放时间超过限制45008 图文消息超过限制45009 接口调用超过限制46001 不存在媒体数据47001 解析JSON/XML内容错误PHP实现1.实例化 获取appid,appsecret
function __construct($appid, $appsecret)
{
$this->appid = $appid ? $appid : C('oauth_config.appid');
$this->appsecret = $appsecret ? $appsecret : C('oauth_config.appsecret');
}
2.获取access_token
/**
* 获取微信基础接口凭证Access_token
* @param $refresh 强制刷新, 默认false
* @return String
*/
function getAccess_token($refresh = false)
{
$data = json_decode(file_get_contents("access_token.json"));

if ($data->expire_time < time() || $refresh) {
$url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$this->appid.'&secret='.$this->appsecret;
$result = json_decode(file_get_contents($url));
$access_token = $result->access_token;

if ($result->errcode && !$access_token) {
$this->error('get access_token failed.');
} else {
$data->expire_time = time() + 7000;
$data->access_token = $access_token;
$fp = fopen("access_token.json", "w");
fwrite($fp, json_encode($data));
fclose($fp);

$this->access_token = $access_token;
}
} else if (!$this->access_token){
$this->access_token = $data->access_token;
}

return $this->access_token;
}
3.发送模板消息
//发送模板消息
function send_template_message($data)
{
$url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=' . $this->getAccess_token();

$result = $this->curlRequest($url, urldecode(json_encode($data)));
return json_decode($result, true);
}
4.组合消息数据
$data = array(
'touser' => $openid, // openid是发送消息的基础
'template_id' => 'JkZGZlvL5ou_UFide5ncZOzLbtUaPPz8cuYdXUKEkzs', // 模板id
'url' => $this->siteUrl . U('Store/Twitter/team'), // 点击跳转地址
'topcolor' => '#FF0000', // 顶部颜色
'data' => array(
'first' => array('value' => '邀请成功'),
'keyword1' => array('value' => $data_arr['realname']),
'keyword2' => array('value' => date('Y年m月d日 H:i', time())),
'remark' => array('value' => '您的好友' . $data_arr['realname'] . '已经成为分销商'),
)
);
5.获取openid,并发送消息
function send($type, $member_id, $data_arr) {
$member = M('Member')->where(array('id' => $member_id))->find();
$wxuser = M('Wxuser')->where(array('id' => $member['wxuser_id']))->find();

//检测用户权限消息设置
if ($member && $wxuser && $this->_checkset($type, $member)) {
$data = $this->_getData($type, $wxuser['openid'], $data_arr);

import('@.Action.WxDevelop');
$tplmsg = new WxTmplmsg(C('PAY_WEIXIN')['appid'], C('PAY_WEIXIN')['appsecret']);
return $tplmsg->send_template_message($data);
}
}
文章来源:http://www.cnblogs.com/jiqing9006/p/5220571.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息