就像这样微信的公众号呀或者服务号呀根据用户的某些操作按照一定格式单独给用户推送的消息,像上图就是一个成为会员的一个通知。
现在来说说这发模板消息怎么发,消息模板下面结合上一篇的获取accress_token 的代码接着往下写。
<?php class wxModel { public $appid; public $secret; public $type; public function __construct() { parent::__construct(); //appid和secret 需取服务号后台去查看 $this->appid = 'AppId'; $this->secret = 'Appsecret'; $this->type = 'client_credential'; } /** * 获取token值, * 获取cookie中的值若cookie失效重新请求微信获取cookie */ public function get_token(){ $access_token = $_COOKIE['access_token']; if(!$access_token){ $access_token = $this->_addToken(); } return $access_token; } /** * 请求微信获取access_token * @return mixed */ private function _addToken () { $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type='.$this->type.'&appid='.$this->appid.'&secret='.$this->secret; $data = json_decode( $this->curl_get($url),true); if($data['errcode']){ //提示报错信息 showMessage($data['errmsg']); } else { setcookie('access_token', $data['access_token'], 7200); return $data['access_token']; } } /** * get请求 * @param $url * @return mixed */ public function curl_get($url){ $ch = curl_init(); //设置选项,包括URL curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HEADER, 0); //执行并获取HTML文档内容 $output = curl_exec($ch); //释放curl句柄 curl_close($ch); //打印获得的数据 return $output; } /** * 会员审核通过通知 * @return bool */ public function sendUserTpl(){ $data = array( 'touser' => '用户的openid需要自己获取', 'first' => '您好,你的审核请求已通过,欢迎使用!', 'keyword1' => '赵七七', 'keyword2' => '138XXXXXXXX', 'keyword3' => date('Y-m-d H:i:s', time()), 'remark' => '最后的那句话' ); $url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=' . $this->get_token(); $str = $this->getUserStr($data); $data = json_decode($this->curl_post($url, $str),true); if($data['errcode'] == 0){ return true; }else{ showMessage($data['errmsg']); } } /** * 审核通知模板所需字符串 * @param $data * @return string */ public function getUserStr($data){ //点击详情跳转的地址 $url = 'http://www.baidu.com'; $template_id = '微信后台所用模板的模板id'; return <<<str { "touser":"{$data['touser']}", "template_id":"{$template_id}", "url":"{$url}", "topcolor":"#FF0000", "data":{ "first": { "value":"{$data['first']}", "color":"#173177" }, "keyword1": { "value":"{$data['keyword1']}", "color":"#173177" }, "keyword2": { "value":"{$data['keyword2']}", "color":"#173177" }, "keyword3": { "value":"{$data['keyword3']}", "color":"#173177" }, "remark": { "value":"{$data['remark']}", "color":"#173177" } } } str; } /** * post 请求 * @param $url * @param $curlPost * @return mixed */ public function curl_post($url,$curlPost){ $ch=curl_init(); curl_setopt($ch, CURLOPT_URL, $url); //设置访问的url地址 curl_setopt($ch, CURLOPT_TIMEOUT, 5); //设置超时 curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); //用户访问代理 User-Agent curl_setopt($ch, CURLOPT_REFERER,$_SERVER['HTTP_HOST']); //设置 referer curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1); //跟踪301 curl_setopt($ch,CURLOPT_POSTFIELDS,$curlPost); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //返回结果 $r=curl_exec($ch); curl_close($ch); return $r; } }调用sendUserTpl方法发送模板消息。
友情提示:垃圾评论一律封号...