PHP code collation for push template messages
Recently, a system that needs to push messages has been worked on, and the push of template messages has been researched. Because of the certified number, it is done with the test number, but the process is basically the same.
This article is based on the official platform of the document written, Http://mp.weixin.qq.com/debug/cgi-bin/readtmpl?t=tmplmsg/faq_tmpl
First, you have to set the format of the template message in the background management, get the ID of a template message
- {First.} DATA}}
- The person who was torn: {{name. DATA}}
- Category of the torn man: {{zu. DATA}}
- Torn: {time. DATA}}
Here to do a tear brand notice for example, the relevant parameters are set up as above. Generate ID fallback.
The following directly post the function that needs to be called Moban () 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 = "" ; //fill in the Backstage AppID
- $appsecret = "" ; //fill in the Backstage 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= ="") //Database query no results get Access_token and deposit
- {
-
- $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 '];
-
- $sql 1 = "INSERT into ' tokentime ' (' id ', ' access_token ', ' time ') VALUES (' $appid ', ' $ACCESS _token ', ' $time ')" ;
- $query 1 =mysql_query ($sql 1);
- }
- 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 '];
-
- $sql 2 = "UPDATE tokentime SET access_token= ' $ACCESS _token ', time= ' $time ' WHERE id= ' $appid '" ;
- $query 2 =mysql_query ($sql 2);
- }
- Else
- {
- $ACCESS _token = $rk [' Access_token '];
- }
- }
- //Template message
- $times = Date (' m ' D Day h:i:s ', Time ());
-
- $template = Array (
- ' Touser ' = $openid ,
- ' template_id ' = "_0DQERSIQPZAB4VJQJJOIPRXZHCVOOFT_390VDHHHVW" , //ID of the template
- ' URL ' = "Http://weixin.qq.com/download" ,
- ' Topcolor ' = "#FF0000" ,
- ' Data ' = Array (
- ' name ' = Array (' value '=>urlencode ($name),' color '= "#00008B" ), //function to pass over the name
- ' zu ' = Array (' value '=>urlencode ($zu),' color '= >' #00008B '), //function to pass over the Zu
- ' Time ' = Array (' value '=>urlencode ($times),' color '= ' #00008B ' ), //Time
- ' remain ' = Array (' value '=>urlencode ($remain),' color ' =' #00008B '),//function to pass over the 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 ' message sent successfully! ' ;
- }
The invocation of a function requires some attention
1, Moban () function is required to pass the parameter, the specific parameters of the
Moban ($name, $zu, $remain, $openid)
$name the man who was torn
Group of people $zu Torn
$remain the remainder of this group
Which OpenID $openid sent to
AppID appserect inside the template must be filled
2, the database must be in the database to build a table, because the access_token validity period of only 7200s, to prevent it from expiring here is the way to save the database, the table name is Tokentime, three fields can be, respectively, the ID (int) time (varchar Access_token (varchar) //parentheses are formatted, access_token fields must be larger
At this point, you can use your own template to send messages to the user, because the sending template message is sent according to OpenID, all need to get the user's OpenID.
When you have time, write about how to get the user's OpenID in bulk, deposit the database, and send template messages and other actions.
http://www.bkjia.com/PHPjc/1010948.html www.bkjia.com true http://www.bkjia.com/PHPjc/1010948.html techarticle push Template message PHP code collation recently done a system that needs to push the message, we have researched the push of the template message. Because of the certified number, just use the test ...