微信公众平台模板消息发送接口文档
為了保證用戶不受到騷擾,在開發(fā)者出現(xiàn)需要主動提醒、通知用戶時,才允許開發(fā)者在公眾平臺網(wǎng)站中模板消息庫中選擇模板,選擇后獲得模板ID,再根據(jù)模板ID向用戶主動推送提醒、通知消息。
模板消息調(diào)用時主要需要模板ID和模板中各參數(shù)的賦值內(nèi)容。請注意:
1.模板中參數(shù)內(nèi)容必須以".DATA"結(jié)尾,否則視為保留字;
2.模板保留符號"{{ }}"
案例:
{{first.DATA}}
買家名字:{{keyword1.DATA}}
付款金額:{{keyword2.DATA}}
下單時間:{{keyword3.DATA}}
{{remark.DATA}}
具體調(diào)用方法
第一步:獲取模板ID
通過在模板消息功能的模板庫中使用需要的模板,可以獲得模板ID。
第二步:請求接口
請注意,URL置空,則在發(fā)送后,點擊模板消息會進入一個空白頁面(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"
}
}
}
效果圖:
事件推送
在模版消息發(fā)送任務(wù)完成后,微信服務(wù)器會將是否送達成功作為通知,發(fā)送到開發(fā)者中心中填寫的服務(wù)器配置地址中。
1、送達成功時,推送的XML如下:
<![CDATA[oia2TjuEGTNoeX76QEjQNrcURxG8]]&g;
1395658920
200163836
2、送達由于用戶拒收(用戶設(shè)置拒絕接收公眾號消息)而失敗時,推送的XML如下:
1395658984
200163840
3、送達由于其他原因失敗時,推送的XML如下:
1395658984
200163840
返回碼說明
在調(diào)用模板消息接口后,會返回JSON數(shù)據(jù)包。正常時的返回JSON數(shù)據(jù)包示例:
{
"errcode":0,
"errmsg":"ok",
"msgid":200228332
}
錯誤時的返回JSON數(shù)據(jù),形式類似,錯誤碼請見本頁下方返回碼說明。
返回碼 說明
-1 系統(tǒng)繁忙
0 請求成功
40001 驗證失敗
40002 不合法的憑證類型
40003 不合法的OpenID
40004 不合法的媒體文件類型
40005 不合法的文件類型
40006 不合法的文件大小
40007 不合法的媒體文件id
40008 不合法的消息類型
40009 不合法的圖片文件大小
40010 不合法的語音文件大小
40011 不合法的視頻文件大小
40012 不合法的縮略圖文件大小
40013 不合法的APPID
41001 缺少access_token參數(shù)
41002 缺少appid參數(shù)
41003 缺少refresh_token參數(shù)
41004 缺少secret參數(shù)
41005 缺少多媒體文件數(shù)據(jù)
41006 access_token超時
42001 需要GET請求
43002 需要POST請求
43003 需要HTTPS請求
44001 多媒體文件為空
44002 POST的數(shù)據(jù)包為空
44003 圖文消息內(nèi)容為空
45001 多媒體文件大小超過限制
45002 消息內(nèi)容超過限制
45003 標(biāo)題字段超過限制
45004 描述字段超過限制
45005 鏈接字段超過限制
45006 圖片鏈接字段超過限制
45007 語音播放時間超過限制
45008 圖文消息超過限制
45009 接口調(diào)用超過限制
46001 不存在媒體數(shù)據(jù)
47001 解析JSON/XML內(nèi)容錯誤
PHP實現(xiàn)
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
/**
* 獲取微信基礎(chǔ)接口憑證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.發(fā)送模板消息
//發(fā)送模板消息
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.組合消息數(shù)據(jù)
$data = array(
'touser' => $openid, // openid是發(fā)送消息的基礎(chǔ)
'template_id' => 'JkZGZlvL5ou_UFide5ncZOzLbtUaPPz8cuYdXUKEkzs', // 模板id
'url' => $this->siteUrl . U('Store/Twitter/team'), // 點擊跳轉(zhuǎn)地址
'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'] . '已經(jīng)成為分銷商'),
)
);
5.獲取openid,并發(fā)送消息
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();
//檢測用戶權(quán)限消息設(shè)置
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);
}
}
總結(jié)
以上是生活随笔為你收集整理的微信公众平台模板消息发送接口文档的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 贪心算法及其理论依据——拟阵
- 下一篇: 四季豆冰箱可以放多久 四季豆的功效与作用