微信的模板消息需要认证的公众号后台申请
申请通过后就可以用平台定义的消息模板了
define('IN_ECS', true); require(dirname(__FILE__) . '/includes/init.php'); require_once(ROOT_PATH . 'includes/cls_json.php'); include_once(dirname(__FILE__) . '/includes/WinxinSdk.class.php'); header('content-type:text/html;charset=utf-8'); ini_set('date.timezone','Asia/Shanghai'); $wxSdk = new WinxinSdk($appID,$appsecret); $GLOBALS['token'] = $wxSdk->getToken();//只是用这个类获取了token GroupErrorNotify();//测试 //团购失败通知 function GroupErrorNotify() { $msgTmp = "1byr4wgBGrrc5s2Dp--G0E7w5lfdrI_2Q0X5PQjQSWQ";//消息模板id /* {{first.DATA}} 拼团商品:{{keyword1.DATA}} 商品金额:{{keyword2.DATA}} 退款金额:{{keyword3.DATA}} {{remark.DATA}} */ $token = $GLOBALS['token'];//获取token $url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token='.$token;//设置url $order_url = "http://www.xxx.com";//订单中心,点击消息跳转的url $groupGoodsTitle = "XXXtest产品失败通知"; $groupGoodsPrice = "¥10.00"; $groupGoodsRefund = "¥5.00";//退款金额 //设置发送的消息 $message = array( 'touser'=>'发送用户的openid',//一定是openid,unionid不行 'template_id'=>$msgTmp, 'url'=>$order_url, 'data'=>array( 'first'=>array('value'=>'您好,本次团购没成功,期待下次您的参与','color'=>'#173177'), 'keyword1'=>array('value'=>$groupGoodsTitle,'color'=>'#173177'), 'keyword2'=>array('value'=>$groupGoodsPrice,'color'=>'#173177'), 'keyword3'=>array('value'=>$groupGoodsRefund,'color'=>'#173177'), 'remark'=>array('value'=>'3个工作日,我们会将进行退款处理,如有任何疑问请联系客服','color'=>'#173177'), ) ); $data = json_encode($message); curlPost($url,$data); return 0;//这个函数如果不return,消息会发送多次 } function curlPost($url,$data = null) {//post请求接口 $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); if (!empty($data)){ curl_setopt($ch, CURLOPT_POST, TRUE); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); } curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($ch); curl_close($ch); return $output; }