自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

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

開(kāi)發(fā) 后端
最近做過(guò)一個(gè)需要推送消息的系統(tǒng),就研究了一下微信的模板消息的推送。由于認(rèn)證過(guò)的微信號(hào),就用測(cè)試號(hào)做的,但是過(guò)程基本一致。

最近做過(guò)一個(gè)需要推送消息的系統(tǒng),就研究了一下微信的模板消息的推送。由于認(rèn)證過(guò)的微信號(hào),就用測(cè)試號(hào)做的,但是過(guò)程基本一致。

[[135718]]

本文基于微信平臺(tái)的官方文檔寫(xiě)成,http://mp.weixin.qq.com/debug/cgi-bin/readtmpl?t=tmplmsg/faq_tmpl

首先,得在微信的后臺(tái)管理中設(shè)置一下,模板消息的格式,獲取到一個(gè)模板消息的id

  1. {{first.DATA}}  
  2. 被撕的人:{{name.DATA}}  
  3. 被撕人的組別:{{zu.DATA}}  
  4. 被撕時(shí)間:{{time.DATA}}  
  5. 本組剩余的人:{{remain.DATA}} 
  6. {{remark.DATA}} 

這里以做的一個(gè)撕名牌的通知為例,相關(guān)參數(shù)的設(shè)置如上。生成id備用。

下面直接貼出需要調(diào)用的函數(shù)moban()  和它的輔助函數(shù)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.  
  16.  $appid="";       //填寫(xiě)微信后臺(tái)的appid 
  17.  $appsecret="";   //填寫(xiě)微信后臺(tái)的appsecret 
  18.  //從數(shù)據(jù)庫(kù)查看access_token 
  19.               $sql="SELECT * FROM `tokentime` WHERE id='$appid'"
  20.               $query=mysql_query($sql); 
  21.               $rk=mysql_fetch_array($query); 
  22.               $time=date('Y-m-d H:i:s',time()); 
  23.               if($rk=="")   //數(shù)據(jù)庫(kù)查詢(xún)無(wú)結(jié)果 獲取access_token并存入 
  24.               { 
  25.                     
  26.                    $TOKEN_URL="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret
  27.                    $json=file_get_contents($TOKEN_URL); 
  28.                    $result=json_decode($json,true); 
  29.                    $ACCESS_TOKEN=$result['access_token'];   
  30.                   
  31.                    $sql1="INSERT INTO `tokentime` (`id`,`access_token`,`time`) VALUES ('$appid','$ACCESS_TOKEN','$time')"
  32.                    $query1=mysql_query($sql1); 
  33.             } 
  34.               else 
  35.               {   $time_b=$rk['time'];//上次存的時(shí)間 
  36.                   $time_n=date('Y-m-d H:i:s',time()-7200); 
  37.                      
  38.                   if($rk['access_token']==""||$time_b<$time_n
  39.                   { 
  40.                     $TOKEN_URL="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret
  41.                    $json=file_get_contents($TOKEN_URL); 
  42.                    $result=json_decode($json,true); 
  43.                    $ACCESS_TOKEN=$result['access_token'];   
  44.                   
  45.                    $sql2="UPDATE tokentime SET access_token='$ACCESS_TOKEN',time='$time' WHERE id='$appid'"
  46.                    $query2=mysql_query($sql2);         
  47.                 } 
  48.                   else 
  49.                   { 
  50.                      $ACCESS_TOKEN=$rk['access_token'];  
  51.                   } 
  52.                 } 
  53. //模板消息     
  54.    $timesdate('m月d日 H:i:s',time());   
  55.        
  56. $template=array
  57. 'touser'=>$openid
  58. 'template_id'=>"_0DQerSIqPZaB4vjQjjOIPRXZhcVooFT_390vDhHhVw",    //模板的id 
  59. 'url'=>"http://weixin.qq.com/download"
  60. 'topcolor'=>"#FF0000"
  61. 'data'=>array
  62. 'name'=>array('value'=>urlencode($name),'color'=>"#00008B"),    //函數(shù)傳參過(guò)來(lái)的name      
  63. 'zu'=>array('value'=>urlencode($zu),'color'=>'#00008B'),        //函數(shù)傳參過(guò)來(lái)的zu 
  64. 'time'=>array('value'=>urlencode($times),'color'=>'#00008B'),   //時(shí)間 
  65. 'remain'=>array('value'=>urlencode($remain),'color'=>'#00008B'),//函數(shù)傳參過(guò)來(lái)的ramain 
  66. ); 
  67. $json_template=json_encode($template); 
  68. $url="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$ACCESS_TOKEN
  69. $res=http_request($url,urldecode($json_template)); 
  70. if ($res[errcode]==0) echo '消息發(fā)送成功!';  

函數(shù)的調(diào)用需要注意幾點(diǎn)

   1、moban()函數(shù)是需要傳參的,具體傳參的

moban($name,$zu,$remain,$openid)   
$name 被撕的人
$zu 被撕的人組別
$remain 本組剩余的人
$openid 發(fā)送給哪個(gè)openid
傳參的可以自行修改 只需要對(duì)應(yīng)上函數(shù)里面模板的輸出格式
模板里面的appid appserect一定要填

2、數(shù)據(jù)庫(kù)的一定在要在數(shù)據(jù)庫(kù)里面建一個(gè)表,因?yàn)閍ccess_token的有效期只有7200s,防止它過(guò)期這里采用了數(shù)據(jù)庫(kù)保存的方式,表名為tokentime,三個(gè)字段就可以了,分別是id(int) time(varchar) access_token(varchar) //括號(hào)里面是格式,access_token字段一定要大一點(diǎn)

至此就可以使用自己的模板給用戶(hù)發(fā)消息了,由于發(fā)送模板消息是按照openid發(fā)送的,所有需要獲取用戶(hù)的openid。
等有時(shí)間,寫(xiě)一下如何批量獲取用戶(hù)的openid,存入數(shù)據(jù)庫(kù),并發(fā)送模板消息和其他操作。
 
責(zé)任編輯:王雪燕 來(lái)源: 博客園
相關(guān)推薦

2013-04-10 18:48:56

微信公眾平臺(tái)技巧

2021-08-10 13:57:37

微信推送技術(shù)

2013-07-22 11:01:24

微信公眾系統(tǒng)故障

2013-07-22 09:24:33

微信系統(tǒng)推送

2018-07-25 13:34:14

Python微信撤回

2025-03-31 10:49:16

2016-11-02 13:12:31

微信離線(xiàn)消息

2025-04-15 09:00:00

2016-10-11 16:31:56

微信服務(wù)器消息

2013-04-08 16:19:40

微信微信公眾平臺(tái)圖文消息

2024-12-19 10:00:00

Python發(fā)送消息編程

2023-12-11 11:16:57

消息模板腳本微信

2020-07-27 15:06:14

微信張小龍焦慮

2013-11-12 23:32:53

微信公號(hào)微信公眾賬號(hào)

2018-06-05 15:04:19

爬蟲(chóng)微信服務(wù)器

2013-08-08 10:13:25

微信

2014-09-24 11:32:21

微信企業(yè)號(hào)開(kāi)發(fā)

2014-09-24 11:11:08

微信企業(yè)號(hào)開(kāi)發(fā)

2018-07-13 16:26:46

編程語(yǔ)言Python微信

2014-09-28 22:34:09

微信企業(yè)號(hào)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)