Home >Backend Development >PHP Tutorial >PHP code arrangement for WeChat push template messages_PHP tutorial
I recently worked on a system that needed to push messages, and I studied the push of WeChat template messages. Since the certified WeChat account is made using a test account, the process is basically the same.
This article is based on the official documentation of the WeChat platform, http://mp.weixin.qq.com/debug/cgi-bin/readtmpl?t=tmplmsg/faq_tmpl
First of all, you have to set the format of the template message in WeChat’s background management and obtain the ID of a template message
<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>
Here is a notification for tearing off a name tag as an example. The relevant parameters are set as above. Generate ID for backup.
The function moban() that needs to be called and its auxiliary function http_request()
are directly posted below.<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>
A few things to note when calling functions
1. The moban() function needs to pass parameters, the specific parameters are passed
<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>