Maison >développement back-end >tutoriel php >微信推送模板消息的PHP代码整理_PHP教程
最近做过一个需要推送消息的系统,就研究了一下微信的模板消息的推送。由于认证过的微信号,就用测试号做的,但是过程基本一致。
本文基于微信平台的官方文档写成,http://mp.weixin.qq.com/debug/cgi-bin/readtmpl?t=tmplmsg/faq_tmpl
首先,得在微信的后台管理中设置一下,模板消息的格式,获取到一个模板消息的id
<ol class="dp-c"><li class="alt"><span><span>{{first.DATA}} </span></span></li><li><span>被撕的人:{{name.DATA}} </span></li><li class="alt"><span>被撕人的组别:{{zu.DATA}} </span></li><li><span>被撕时间:{{time.DATA}} </span></li><li class="alt"><span>本组剩余的人:{{remain.DATA}} </span></li><li><span>{{remark.DATA}} </span></li></ol>
这里以做的一个撕名牌的通知为例,相关参数的设置如上。生成id备用。
下面直接贴出需要调用的函数moban() 和它的辅助函数http_request()
<ol class="dp-c"><li class="alt"><span><span>http_request(){ </span></span></li><li><span><span class="vars">$ch</span><span> = curl_init(); </span></span></li><li class="alt"><span>curl_setopt(<span class="vars">$ch</span><span>, CURLOPT_URL, </span><span class="vars">$url</span><span>); </span></span></li><li><span>curl_setopt(<span class="vars">$ch</span><span>, CURLOPT_RETURNTRANSFER, 1); </span></span></li><li class="alt"><span>curl_setopt(<span class="vars">$ch</span><span>, CURLOPT_SSL_VERIFYPEER, FALSE); </span></span></li><li><span>curl_setopt(<span class="vars">$ch</span><span>, CURLOPT_SSL_VERIFYHOST, FALSE); </span></span></li><li class="alt"><span>curl_setopt(<span class="vars">$ch</span><span>, CURLOPT_POST, 1); </span></span></li><li><span>curl_setopt(<span class="vars">$ch</span><span>, CURLOPT_POSTFIELDS, </span><span class="vars">$data</span><span>); </span></span></li><li class="alt"><span><span class="vars">$output</span><span> = curl_exec(</span><span class="vars">$ch</span><span>); </span></span></li><li><span>curl_close(<span class="vars">$ch</span><span>); </span></span></li><li class="alt"><span><span class="keyword">return</span><span> </span><span class="vars">$output</span><span>; </span></span></li><li><span>} </span></li><li class="alt"><span> </span></li><li><span> <span class="keyword">function</span><span> moban(</span><span class="vars">$name</span><span>,</span><span class="vars">$zu</span><span>,</span><span class="vars">$remain</span><span>,</span><span class="vars">$openid</span><span>) </span></span></li><li class="alt"><span> { </span></li><li><span> </span></li><li class="alt"><span> <span class="vars">$appid</span><span>=</span><span class="string">""</span><span>; </span><span class="comment">//填写微信后台的appid</span><span> </span></span></li><li><span> <span class="vars">$appsecret</span><span>=</span><span class="string">""</span><span>; </span><span class="comment">//填写微信后台的appsecret</span><span> </span></span></li><li class="alt"><span> <span class="comment">//从数据库查看access_token</span><span> </span></span></li><li><span> <span class="vars">$sql</span><span>=</span><span class="string">"SELECT * FROM `tokentime` WHERE id='$appid'"</span><span>; </span></span></li><li class="alt"><span> <span class="vars">$query</span><span>=mysql_query(</span><span class="vars">$sql</span><span>); </span></span></li><li><span> <span class="vars">$rk</span><span>=mysql_fetch_array(</span><span class="vars">$query</span><span>); </span></span></li><li class="alt"><span> <span class="vars">$time</span><span>=</span><span class="func">date</span><span>(</span><span class="string">'Y-m-d H:i:s'</span><span>,time()); </span></span></li><li><span> <span class="keyword">if</span><span>(</span><span class="vars">$rk</span><span>==</span><span class="string">""</span><span>) </span><span class="comment">//数据库查询无结果 获取access_token并存入</span><span> </span></span></li><li class="alt"><span> { </span></li><li><span> </span></li><li class="alt"><span> <span class="vars">$TOKEN_URL</span><span>=</span><span class="string">"https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="</span><span>.</span><span class="vars">$appid</span><span>.</span><span class="string">"&secret="</span><span>.</span><span class="vars">$appsecret</span><span>; </span></span></li><li><span> <span class="vars">$json</span><span>=</span><span class="func">file_get_contents</span><span>(</span><span class="vars">$TOKEN_URL</span><span>); </span></span></li><li class="alt"><span> <span class="vars">$result</span><span>=json_decode(</span><span class="vars">$json</span><span>,true); </span></span></li><li><span> <span class="vars">$ACCESS_TOKEN</span><span>=</span><span class="vars">$result</span><span>[</span><span class="string">'access_token'</span><span>]; </span></span></li><li class="alt"><span> </span></li><li><span> <span class="vars">$sql1</span><span>=</span><span class="string">"INSERT INTO `tokentime` (`id`,`access_token`,`time`) VALUES ('$appid','$ACCESS_TOKEN','$time')"</span><span>; </span></span></li><li class="alt"><span> <span class="vars">$query1</span><span>=mysql_query(</span><span class="vars">$sql1</span><span>); </span></span></li><li><span> } </span></li><li class="alt"><span> <span class="keyword">else</span><span> </span></span></li><li><span> { <span class="vars">$time_b</span><span>=</span><span class="vars">$rk</span><span>[</span><span class="string">'time'</span><span>];</span><span class="comment">//上次存的时间</span><span> </span></span></li><li class="alt"><span> <span class="vars">$time_n</span><span>=</span><span class="func">date</span><span>(</span><span class="string">'Y-m-d H:i:s'</span><span>,time()-7200); </span></span></li><li><span> </span></li><li class="alt"><span> <span class="keyword">if</span><span>(</span><span class="vars">$rk</span><span>[</span><span class="string">'access_token'</span><span>]==</span><span class="string">""</span><span>||</span><span class="vars">$time_b</span><span><</span><span class="vars">$time_n</span><span>) </span></span></li><li><span> { </span></li><li class="alt"><span> <span class="vars">$TOKEN_URL</span><span>=</span><span class="string">"https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="</span><span>.</span><span class="vars">$appid</span><span>.</span><span class="string">"&secret="</span><span>.</span><span class="vars">$appsecret</span><span>; </span></span></li><li><span> <span class="vars">$json</span><span>=</span><span class="func">file_get_contents</span><span>(</span><span class="vars">$TOKEN_URL</span><span>); </span></span></li><li class="alt"><span> <span class="vars">$result</span><span>=json_decode(</span><span class="vars">$json</span><span>,true); </span></span></li><li><span> <span class="vars">$ACCESS_TOKEN</span><span>=</span><span class="vars">$result</span><span>[</span><span class="string">'access_token'</span><span>]; </span></span></li><li class="alt"><span> </span></li><li><span> <span class="vars">$sql2</span><span>=</span><span class="string">"UPDATE tokentime SET access_token='$ACCESS_TOKEN',time='$time' WHERE id='$appid'"</span><span>; </span></span></li><li class="alt"><span> <span class="vars">$query2</span><span>=mysql_query(</span><span class="vars">$sql2</span><span>); </span></span></li><li><span> } </span></li><li class="alt"><span> <span class="keyword">else</span><span> </span></span></li><li><span> { </span></li><li class="alt"><span> <span class="vars">$ACCESS_TOKEN</span><span>=</span><span class="vars">$rk</span><span>[</span><span class="string">'access_token'</span><span>]; </span></span></li><li><span> } </span></li><li class="alt"><span> } </span></li><li><span><span class="comment">//模板消息 </span><span> </span></span></li><li class="alt"><span> <span class="vars">$times</span><span>= </span><span class="func">date</span><span>(</span><span class="string">'m月d日 H:i:s'</span><span>,time()); </span></span></li><li><span> </span></li><li class="alt"><span><span class="vars">$template</span><span>=</span><span class="keyword">array</span><span>( </span></span></li><li><span><span class="string">'touser'</span><span>=></span><span class="vars">$openid</span><span>, </span></span></li><li class="alt"><span><span class="string">'template_id'</span><span>=></span><span class="string">"_0DQerSIqPZaB4vjQjjOIPRXZhcVooFT_390vDhHhVw"</span><span>, </span><span class="comment">//模板的id</span><span> </span></span></li><li><span><span class="string">'url'</span><span>=></span><span class="string">"http://weixin.qq.com/download"</span><span>, </span></span></li><li class="alt"><span><span class="string">'topcolor'</span><span>=></span><span class="string">"#FF0000"</span><span>, </span></span></li><li><span><span class="string">'data'</span><span>=></span><span class="keyword">array</span><span>( </span></span></li><li class="alt"><span><span class="string">'name'</span><span>=></span><span class="keyword">array</span><span>(</span><span class="string">'value'</span><span>=>urlencode(</span><span class="vars">$name</span><span>),</span><span class="string">'color'</span><span>=></span><span class="string">"#00008B"</span><span>), </span><span class="comment">//函数传参过来的name </span><span> </span></span></li><li><span><span class="string">'zu'</span><span>=></span><span class="keyword">array</span><span>(</span><span class="string">'value'</span><span>=>urlencode(</span><span class="vars">$zu</span><span>),</span><span class="string">'color'</span><span>=></span><span class="string">'#00008B'</span><span>), </span><span class="comment">//函数传参过来的zu</span><span> </span></span></li><li class="alt"><span><span class="string">'time'</span><span>=></span><span class="keyword">array</span><span>(</span><span class="string">'value'</span><span>=>urlencode(</span><span class="vars">$times</span><span>),</span><span class="string">'color'</span><span>=></span><span class="string">'#00008B'</span><span>), </span><span class="comment">//时间</span><span> </span></span></li><li><span><span class="string">'remain'</span><span>=></span><span class="keyword">array</span><span>(</span><span class="string">'value'</span><span>=>urlencode(</span><span class="vars">$remain</span><span>),</span><span class="string">'color'</span><span>=></span><span class="string">'#00008B'</span><span>),</span><span class="comment">//函数传参过来的ramain</span><span> </span></span></li><li class="alt"><span>) </span></li><li><span>); </span></li><li class="alt"><span><span class="vars">$json_template</span><span>=json_encode(</span><span class="vars">$template</span><span>); </span></span></li><li><span><span class="vars">$url</span><span>=</span><span class="string">"https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="</span><span>.</span><span class="vars">$ACCESS_TOKEN</span><span>; </span></span></li><li class="alt"><span><span class="vars">$res</span><span>=http_request(</span><span class="vars">$url</span><span>,urldecode(</span><span class="vars">$json_template</span><span>)); </span></span></li><li><span><span class="keyword">if</span><span> (</span><span class="vars">$res</span><span>[errcode]==0) </span><span class="func">echo</span><span> </span><span class="string">'消息发送成功!'</span><span>; </span></span></li><li class="alt"><span>} </span></li></ol>
函数的调用需要注意几点
1、moban()函数是需要传参的,具体传参的
<em id="__mceDel">moban($name,$zu,$remain,$openid<span>) <br />$name 被撕的人<br />$zu 被撕的人组别<br />$remain 本组剩余的人<br />$openid 发送给哪个openid<br />传参的可以自行修改 只需要对应上函数里面模板的输出格式 <br />模板里面的appid appserect一定要填<br /><br /></span></em><span style="font-size: 15px;">2、数据库的一定在要在数据库里面建一个表,因为access_token的有效期只有7200s,防止它过期这里采用了数据库保存的方式,表名为tokentime,三个字段就可以了,分别是id(int) time(varchar) access_token(varchar) //括号里面是格式,access_token字段一定要大一点<br /><br />至此就可以使用自己的模板给用户发消息了,由于发送模板消息是按照openid发送的,所有需要获取用户的openid。<br />等有时间,写一下如何批量获取用户的openid,存入数据库,并发送模板消息和其他操作。</span>