总体思路:
1.获取access_token
<pre name="code" class="php">
<span style="white-space:pre"> </span> $appsecret = 应用密钥;
$token = Token(令牌);
if($token){
$auth = new \Com\WechatAuth($appid, $appsecret, $token);
}else{
$auth = new \Com\WechatAuth($appid, $appsecret);
$token = $auth->getAccessToken();
S("wx_tonken", $token['access_token'],$token['expires_in']);
}
2.拼接POST字符串
$data = array() ;
$data['touser'] = $openid;
$data['template_id'] = 'aAH03LjLxwOVvBTazNyEgGGHMzS9iQ-b9ssTghH-sQY' ;
$data['url'] = 'http://www.baidu.com' ;
$data['topcolor'] = '#ad0a05' ;
$data['data'] = array(
'first'=>array(
'value'=>'消息模板测试11',
'color'=>'#ad0a05'
),
'orderMoneySum'=>array(
'value'=>'1元',
'color'=>'#ad0a05'
),
'orderProductName'=>array(
'value'=>'1元抢购枸杞',
'color'=>'#ad0a05'
),
'Remark'=>array(
'value'=>'如有问题请致电400-xxxx-xxxx或直接在微信留言,xxx将第一时间为您服务!',
'color'=>'#ad0a05'
),
);
$data = json_encode($data) ;
我的模板是TM00015 , 数据的key 看看根据自己的模板来写 ,不知道的可以自行百度 , 还有就是模板消息 ,需要在微信公众平台插件库先开通
3.curl 执行 , 发送数据给微信 ,让微信给openid对应的用户推送消息
$url = "https://api.weixin.qq.com/cgi-bin/message/template/send";
$param = array('access_token'=>$token);
$rs = WechatAuth::http($url,$param,$data,'POST');
还是TP的wechatAuth的 静态方法 , 原始类中 http方法是protected,改成public就可以在外部调用了
4.随便扔到一个action 中 ,就可以执行了
备注: 1.如有疑问请参照 https://mp.weixin.qq.com/advanced/tmplmsg?action=faq&token=1694228754&lang=zh_CN
2.如使用其他框架 , 实现要curl的POST方法 ,替代文中的 http 方法
3.文中的openid可以通过微信网页授权微信网页授权获取 ,后续补上 。。。