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


本文為本人原創,未經許可,不可轉載。  博主長期從事微信開發,微信開發相關問題和業務請聯系qq 2580234897

最近做過一個需要推送消息的系統,就研究了一下微信的模板消息的推送。由於認證過的微信號,就用測試號做的,但是過程基本一致。

  本文基於微信平台的官方文檔寫成,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。
等有時間,寫一下如何批量獲取用戶的openid,存入數據庫,並發送模板消息和其他操作。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM