The PHP code collation of micro-letter push Template message

Source: Internet
Author: User
Tags date format array sql openid php code table name urlencode





Recently did a need to push the message system, studied the micro-letter template message push. Because of the certified micro-signal, it is done with the test number, but the process is basically the same.






This article is based on the micro-letter platform of the official document written, Http://mp.weixin.qq.com/debug/cgi-bin/readtmpl?t=tmplmsg/faq_tmpl



First of all, in the micro-mail background management set up, template message format, get the ID of a template message



 
  


  
  1. {{A. DATA}}
  2. The person who was torn: {{name. DATA}}
  3. Group of torn persons: {zu. DATA}}
  4. Time to be torn: {{times. DATA}}
  5. The remaining people in this group: {{remain. DATA}}
  6. {{remark. DATA}}


Here to do a tear-brand notice as an example, the relevant parameters are set as above. Generate ID standby.



The function Moban () and its auxiliary function http_request () are posted directly below.


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 = ""; // Fill in the appid of WeChat background
 $ appsecret = ""; // Fill in appsecret on WeChat background
 // View access_token from the database
              $ 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 == "") // No result from database query Get access_token and save
              {
                    
                   $ 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']; // Last saved 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'];
                  }
                }
// Template message
   $ times = date ('m month d H: i: s', time ());
       
$ template = array (
'touser' => $ openid,
'template_id' => "_ 0DQerSIqPZaB4vjQjjOIPRXZhcVooFT_390vDhHhVw", // id of template
'url' => "http://weixin.qq.com/download",
'topcolor' => "# FF0000",
'data' => array (
'name' => array ('value' => urlencode ($ name), 'color' => "# 00008B"), // The name passed by the function
'zu' => array ('value' => urlencode ($ zu), 'color' => '# 00008B'), // The zu passed by the function
'time' => array ('value' => urlencode ($ times), 'color' => '# 00008B'), // time
'remain' => array ('value' => urlencode ($ remain), 'color' => '# 00008B'), // main
)
);
$ 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 'Message sent successfully!';
}


The call to the function requires some attention

1, Moban () function is required to pass the reference, the specific parameters of the


moban ($ name, $ zu, $ remain, $ openid)
$ name torn person
$ zu torn person group
$ remain Remaining people in this group
$ openid to which openid to send
The parameters can be modified by themselves, just need to correspond to the output format of the template in the function
The appid appserect in the template must be filled

2, the database must build a table in the database, because the access_token is only valid for 7200s, to prevent it from expired here, the database is saved, the table name is tokentime, three fields can be, id (int) time (varchar) access_token (varchar) // The format is in parentheses, the access_token field must be larger

At this point, you can use your own template to send messages to users. Since the template message is sent according to openid, all users need to obtain the user's openid.
When you have time, write about how to get the user's openid in batches, store it in the database, and send template messages and other operations.




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.