您的位置:首页 > 移动开发 > 微信开发

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

2017-10-24 10:04 686 查看
最近做过一个需要推送消息的系统,就研究了一下微信的模板消息的推送。由于认证过的微信号,就用测试号做的,但是过程基本一致。





本文基于微信平台的官方文档写成,http://mp.weixin.qq.com/debug/cgi-bin/readtmpl?t=tmplmsg/faq_tmpl

首先,得在微信的后台管理中设置一下,模板消息的格式,获取到一个模板消息的id

{{first.DATA}}  
被撕的人:{{name.DATA}}  
被撕人的组别:{{zu.DATA}}  
被撕时间:{{time.DATA}}  
本组剩余的人:{{remain.DATA}} 
{{remark.DATA}} 
这里以做的一个撕名牌的通知为例,相关参数的设置如上。生成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; 

 
  function moban($name,$zu,$remain,$openid) 
  { 
 
 $appid="";       //填写微信后台的appid 
 $appsecret="";   //填写微信后台的appsecret 
 //从数据库查看access_token 
              $sql="SELECT * FROM `tokentime` WHERE id='$appid'"; 
              $query=mysql_query($sql); 
              $rk=mysql_fetch_array($query); 
              $time=date('Y-m-d H: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="INSERT INTO `tokentime` (`id`,`access_token`,`time`) VALUES ('$appid','$ACCESS_TOKEN','$time')"; 
                   $query1=mysql_query($sql1); 
            } 
              else 
              {   $time_b=$rk['time'];//上次存的时间 
                  $time_n=date('Y-m-d H: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="UPDATE tokentime SET access_token='$ACCESS_TOKEN',time='$time' WHERE id='$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 appserect一定要填

2、数据库的一定在要在数据库里面建一个表,因为access_token的有效期只有7200s,防止它过期这里采用了数据库保存的方式,表名为tokentime,三个字段就可以了,分别是id(int) time(varchar) access_token(varchar)    //括号里面是格式,access_token字段一定要大一点

至此就可以使用自己的模板给用户发消息了,由于发送模板消息是按照openid发送的,所有需要获取用户的openid。

等有时间,写一下如何批量获取用户的openi
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: