1、所有服务号都可以在功能->添加功能插件处看到申请模板消息功能的入口,但只有认证后的服务号才可以申请模板消息的使用权限并获得该权限;
2、需要选择公众账号服务所处的2个行业,每月可更改1次所选行业;
3、在所选择行业的模板库中选用已有的模板进行调用;
4、每个账号可以同时使用25个模板。
5、当前每个账号的模板消息的日调用上限为10万次,单个模板没有特殊限制。【2014年11月18日将接口调用频率从默认的日1万次提升为日10万次,可在MP登录后的开发者中心查看】。当账号粉丝数超过10W/100W/1000W时,模板消息的日调用上限会相应提升,以公众号MP后台开发者中心页面中标明的数字为准。
有时支付完这后,公众号会发送支付通知你。类似这种的通知
微信官方接口如下:
接口调用请求说明
http请求方式: POST
https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN
POST数据说明
POST数据示例如下:
{
"touser":"OPENID",
"template_id":"ngqIpbwh8bUfcSsECmogfXcV14J0tQlEpBO27izEYtY",
"url":"http://weixin.qq.com/download",
"miniprogram":{
"appid":"xiaochengxuappid12345",
"pagepath":"index?foo=bar"
},
"data":{
"first": {
"value":"恭喜你购买成功!",
"color":"#173177"
},
"keyword1":{
"value":"巧克力",
"color":"#173177"
},
"keyword2": {
"value":"39.8元",
"color":"#173177"
},
"keyword3": {
"value":"2014年9月22日",
"color":"#173177"
},
"remark":{
"value":"欢迎再次购买!",
"color":"#173177"
}
}
}
第一步要自己设置添加消息模板
我贴出前端代码 (当点击发送按钮的时候就发送消息模板)
function send(){
var openid = $("#openid").val();
$.ajax({
url:"{:U('Index/send')}",
data:{"openid":openid},
dataType:"JSON",
type:"POST",
success:function(res){
var res=JSON.parse(res);
if(res.errmsg=="ok")
{
alert("发送成功!");
}
}
})
后端代码get_access_token 是自己定义的函数,获取access_token, http_request自己定义的函数curl请求 都是写在functions.php里面
//消息模板
function send(){ $openid=I("post.openid"); $access_token=get_access_token($this->appid,$this->appSecret); $url="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=$access_token"; $data = array( "touser"=>$openid, "template_id"=>"W72DBlYFJ1CF7ddfc4-7t8sWtKAxYoxHA9mdgKZLAYs", "url"=>"http://minshu.xin/My/index.php/Home/Index/share", "data"=> array( "content" => array( "value"=>"很感谢您接受了消息模板!", "color"=>"#173177", ), "keyword1" =>array( "value"=>"测试专用", "color"=>"#173177" ), "keyword2"=>array( "value"=>date("Y年m月d日",time()), "color"=>"#173177", ), "color"=> array( "value"=>"欢迎再次接收!", "color"=>"#173177", ), ), ); $data = json_encode($data); $info=http_request($url, $data); $this->ajaxReturn($info); }
functions.php /** * 获取access_token * @param mixed $appid 公众号id * @param mixed $secret 公众号秘钥 * @return mixed $access_token */ function get_access_token($appid,$appsecret){ $appid=$appid; $appsecret=$appsecret; $url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appsecret"; $where= array( "appid" =>$appid, "appsecret"=>$appsecret ); $info=M("access_token")->where($where)->find(); if(!$info){ $access_token=http_request($url); $data=json_decode($access_token,true); $add["access_token"]=$data["access_token"]; $add["appid"] =$appid; $add["appsecret"]=$appsecret; $add["timestamp"]=time(); M("access_token")->add($add); $access_token=$add["access_token"]; }elseif(($info["timestamp"]+7000)<time()){ $access_token=http_request($url); $data=json_decode($access_token,true); $save["access_token"]=$data["access_token"]; $save["timestamp"]=time(); M("access_token")->where($where)->save($save); $access_token=$save["access_token"]; }else{ $access_token=$info["access_token"]; } return $access_token; } /** * 获取和设置配置参数 支持批量定义 * @param $url 请求url * @param $data 发送数据 * @param $header 协议头 * @return $output 资源 */ function http_request($url,$data = null,$headers=array()) { $curl =curl_init(); if( count($headers) >= 1 ){ curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); } curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); if (!empty($data)){ curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); } curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($curl); curl_close($curl); return $output; }
效果图: