当前位置: 首页 > 科技观察

微信推送模板消息PHP代码整理

时间:2023-03-18 12:35:48 科技观察

最近在做一个需要推送消息的系统,于是研究了一下微信模板消息的推送。因为认证了微信号,所以是用测试号做的,但是流程基本一样。本文是根据微信平台官方文档编写的,http://mp.weixin.qq.com/debug/cgi-bin/readtmpl?t=tmplmsg/faq_tmpl首先,你要设置模板的格式在微信后台管理消息,获取一条模板消息的id{{first.DATA}}被撕的人:{{name.DATA}}被撕的人所在的组:{{zu.DATA}}DATA}}撕掉时间:{{time.DATA}}}本组剩余人数:{{remain.DATA}}{{remark.DATA}}下面是撕掉牌子的告示示例name,相关参数如上设置。生成用于备份的id。需要调用的函数moban()及其辅助函数http_request()http_request(){$ch=curl_init();curl_setopt($ch,CURLOPT_URL,$url);curl_setopt($ch,CURLOPT_RETURNTRANSFER,1)直接贴在下面;curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);curl_setopt($ch,CURLOPT_POST,1);curl_setopt($ch,CURLOPT_POSTFIELDS,$data);$output=curl_exec($ch);curl_close($ch);return$output;}functionmoban($name,$zu,$remain,$openid){$appid="";//填写微信后台appid$appsecret="";//填写微信后台appsecret//从数据库中查看access_token$sql="SELECT*FROM`tokentime`WHEREid='$appid'";$query=mysql_query($sql);$rk=mysql_fetch_array($query);$time=date('Y-m-dH:i:s',time());if($rk=="")//数据库查询无结果,获取access_token保存在{$TOKEN_URL="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret;$json=file_get_contents($TOKEN_URL);$result=json_decode($json,true);$ACCESS_TOKEN=$result['access_token'];$sql1=”t;INSERTINTO`tokentime`(`id`,`access_token`,`time`)VALUES('$appid','$ACCESS_TOKEN','$time')";$query1=mysql_query($sql1);}else{$time_b=$rk['time'];//上次存的时间$time_n=date('Y-m-dH:i:s',time()-7200);if($rk['access_token']==""||$time_b<$time_n){$TOKEN_URL="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret;$json=file_get_contents($TOKEN_URL);$result=json_decode($json,true);$ACCESS_TOKEN=$result['access_token'];$sql2="UPDATEtokentimeSETaccess_token='$ACCESS_TOKEN',time='$time'WHEREid='$appid'";$query2=mysql_query($sql2);}else{$ACCESS_TOKEN=$rk['access_token'];}}//模板消息$times=date('m月d日H:i:s',time());$template=array('touser'=>$openid,'template_id'=>"_0DQerSIqPZaB4vjQjjOIPRXZhcVooFT_390vDhHhVw",//模板的id'url'=>"http://weixin.qq.com/download",'topcolor'=>"#FF0000",'data'=>array('name'=>array('value'=>urlencode($name),'color'=>"#00008B"),//name'zu'=>array('value'=>urlencode($zu),'color'=>'#00008B'),//zu'time'=>array('value'=>urlencode($times),'color'=>'#00008B'),//时间'remain'=>array('value'=>urlencode($remain),'color'=>'#00008B'),//函数传过来的ramain));$json_template=json_encode($template);$url="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$ACCESS_TOKEN;$res=http_request($url,urldecode($json_template));if($res[errcode]==0)echo'消息发送成功!';}函数调用有几点需要注意1.moban()函数需要传参,具体参数moban($name,$zu,$remain,$openid)$name撕人$zu撕人组$remain这个组剩下的人$openid传给哪个openid传参,可以自己修改,只需要对应函数中模板的输出格式,appid中的appserecttemplate必须填写2,database必须在数据库中建表,因为access_token的有效期只有7200s,为了防止过期,这里采用数据库存储的方式,表名是tokentime,三个字段就够了,即id(int)time(varchar)access_token(varchar)//括号内的格式,access_token字段必须大一些,这样才能使用自己的模板给用户发送消息,由于模板消息是根据openid发送的,所有需要获取用户的openid有时间写一下如何批量获取用户的openid,存入数据库,发送模板消息等操作。