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

原创
2016-06-21 08:46:44 907浏览

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

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

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

  1. {{first.DATA}}
  2. 被撕的人:{{name.DATA}}
  3. 被撕人的组别:{{zu.DATA}}
  4. 被撕时间:{{time.DATA}}
  5. 本组剩余的人:{{remain.DATA}}
  6. {{remark.DATA}}

这里以做的一个撕名牌的通知为例,相关参数的设置如上。生成id备用。

下面直接贴出需要调用的函数moban() 和它的辅助函数http_request()

  1. http_request(){
  2. $ch = curl_init();
  3. curl_setopt($ch, CURLOPT_URL, $url);
  4. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  5. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  6. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  7. curl_setopt($ch, CURLOPT_POST, 1);
  8. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  9. $output = curl_exec($ch);
  10. curl_close($ch);
  11. return $output;
  12. }
  13. function moban($name,$zu,$remain,$openid)
  14. {
  15. $appid=""; //填写微信后台的appid
  16. $appsecret=""; //填写微信后台的appsecret
  17. //从数据库查看access_token
  18. $sql="SELECT * FROM `tokentime` WHERE id='$appid'";
  19. $query=mysql_query($sql);
  20. $rk=mysql_fetch_array($query);
  21. $time=date('Y-m-d H:i:s',time());
  22. if($rk=="") //数据库查询无结果 获取access_token并存入
  23. {
  24. $TOKEN_URL="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret;
  25. $json=file_get_contents($TOKEN_URL);
  26. $result=json_decode($json,true);
  27. $ACCESS_TOKEN=$result['access_token'];
  28. $sql1="INSERT INTO `tokentime` (`id`,`access_token`,`time`) VALUES ('$appid','$ACCESS_TOKEN','$time')";
  29. $query1=mysql_query($sql1);
  30. }
  31. else
  32. { $time_b=$rk['time'];//上次存的时间
  33. $time_n=date('Y-m-d H:i:s',time()-7200);
  34. if($rk['access_token']==""$time_b<$time_n)
  35. {
  36. $TOKEN_URL="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret;
  37. $json=file_get_contents($TOKEN_URL);
  38. $result=json_decode($json,true);
  39. $ACCESS_TOKEN=$result['access_token'];
  40. $sql2="UPDATE tokentime SET access_token='$ACCESS_TOKEN',time='$time' WHERE id='$appid'";
  41. $query2=mysql_query($sql2);
  42. }
  43. else
  44. {
  45. $ACCESS_TOKEN=$rk['access_token'];
  46. }
  47. }
  48. //模板消息
  49. $times= date('m月d日 H:i:s',time());
  50. $template=array(
  51. 'touser'=>$openid,
  52. 'template_id'=>"_0DQerSIqPZaB4vjQjjOIPRXZhcVooFT_390vDhHhVw", //模板的id
  53. 'url'=>"http://weixin.qq.com/download",
  54. 'topcolor'=>"#FF0000",
  55. 'data'=>array(
  56. 'name'=>array('value'=>urlencode($name),'color'=>"#00008B"), //函数传参过来的name
  57. 'zu'=>array('value'=>urlencode($zu),'color'=>'#00008B'), //函数传参过来的zu
  58. 'time'=>array('value'=>urlencode($times),'color'=>'#00008B'), //时间
  59. 'remain'=>array('value'=>urlencode($remain),'color'=>'#00008B'),//函数传参过来的ramain
  60. )
  61. );
  62. $json_template=json_encode($template);
  63. $url="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$ACCESS_TOKEN;
  64. $res=http_request($url,urldecode($json_template));
  65. if ($res[errcode]==0) echo '消息发送成功!';
  66. }

函数的调用需要注意几点

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。
等有时间,写一下如何批量获取用户的openid,存入数据库,并发送模板消息和其他操作。



声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。