PHP code for pushing template messages
I have recently worked on a system that needs to push messages. I have studied how to push template messages. Because of the authenticated number, the test number is used, but the process is basically the same.
This article based on the official documentation written platform, http://mp.weixin.qq.com/debug/cgi-bin/readtmpl? T = tmplmsg/faq_tmpl
First, you must set the template Message format in the background management to get the id of a template message.
- {First. DATA }}
- Torn by: {name. DATA }}
- Target group: {zu. DATA }}
- Time torn: {time. DATA }}
- Remaining persons in this group: {remain. DATA }}
- {Remark. DATA }}
Here, we take a Notice of tearing a famous brand as an example. The relevant parameter settings are as follows. Generate id backup.
Below we will directly post the function moban () to be called and its auxiliary function 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 = ""; // enter the background appid.
- $ Appsecret = ""; // enter the background appsecret
- // 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 = "") // The database query does not return any results. Obtain the access_token and save it
- {
- $ 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 stored 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 day H: I: s', time ());
- $ Template = array (
- 'Touser' => $ openid,
- 'Template _ id' => "_ 0DQerSIqPZaB4vjQjjOIPRXZhcVooFT_390vDhHhVw", // template id
- 'Url' => "http://weixin.qq.com/download ",
- 'Topcolor' => "# FF0000 ",
- 'Data' => array (
- 'Name' => array ('value' => urlencode ($ name), 'color' => "# 00008B"), // name of the parameter passed by the function
- 'Zu' => array ('value' => urlencode ($ zu), 'color' => '# 00008B'), // zu passed by the function
- 'Time' => array ('value' => urlencode ($ times), 'color' => '# hour 8B'), // time
- 'Remain' => array ('value' => urlencode ($ remain), 'color' => '# comment 8B'), // ramain passed by the function
- )
- );
- $ 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! ';
- }
Notes for function calls
1. The moban () function requires parameter passing.
Moban ($ name, $ zu, $ remain, $ openid)
$ Name torn
$ Zu torn Group
$ Remain: remaining persons in this group
$ Openid
You can modify the output format of the template in the function.
The appid appserect in the template must be filled in
2. The database must create a table in the database, because the validity period of access_token is only 7200 s. To prevent it from expiration, the database is saved. The table name is tokentime, the three fields are available, namely id (int) time (varchar) access_token (varchar) // The format is in brackets, and the access_token field must be larger.
Now you can use your own template to send messages to users. Since the template message is sent according to the openid, all users need to obtain the user's openid.
If you have time, write down how to obtain your openid in batches, store it to the database, and send template messages and other operations.