PHP code for pushing template messages on WeChat

Source: Internet
Author: User

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.

 
 
  1. {First. DATA }}
  2. Torn by: {name. DATA }}
  3. Target group: {zu. DATA }}
  4. Time torn: {time. DATA }}
  5. Remaining persons in this group: {remain. DATA }}
  6. {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 ()

 
 
  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.  
  14. Function moban ($ name, $ zu, $ remain, $ openid)
  15. {
  16.  
  17. $ Appid = ""; // enter the background appid.
  18. $ Appsecret = ""; // enter the background appsecret
  19. // View access_token from the database
  20. $ SQL = "SELECT * FROM 'tokentime' WHERE id = '$ appid '";
  21. $ Query = mysql_query ($ SQL );
  22. $ Rk = mysql_fetch_array ($ query );
  23. $ Time = date ('Y-m-d H: I: s', time ());
  24. If ($ rk = "") // The database query does not return any results. Obtain the access_token and save it
  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. $ Sql1 = "insert into 'tokentime' ('id', 'Access _ token', 'time') VALUES ('$ appid', '$ ACCESS_TOKEN ', '$ Time ')";
  31. $ Query1 = mysql_query ($ sql1 );
  32. }
  33. Else
  34. {$ Time_ B = $ rk ['time']; // last stored time
  35. $ Time_n = date ('Y-m-d H: I: s', time ()-7200 );
  36. If ($ rk ['Access _ token'] = "" | $ time_ B <$ time_n)
  37. {
  38. $ TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token? Grant_type = client_credential & appid = ". $ appid." & secret = ". $ appsecret;
  39. $ Json = file_get_contents ($ TOKEN_URL );
  40. $ Result = json_decode ($ json, true );
  41. $ ACCESS_TOKEN = $ result ['Access _ token'];
  42. $ Sql2 = "UPDATE tokentime SET access_token = '$ access_token', time =' $ time' WHERE id = '$ appid '";
  43. $ Query2 = mysql_query ($ sql2 );
  44. }
  45. Else
  46. {
  47. $ ACCESS_TOKEN = $ rk ['Access _ token'];
  48. }
  49. }
  50. // Template message
  51. $ Times = date ('m month D day H: I: s', time ());
  52. $ Template = array (
  53. 'Touser' => $ openid,
  54. 'Template _ id' => "_ 0DQerSIqPZaB4vjQjjOIPRXZhcVooFT_390vDhHhVw", // template id
  55. 'Url' => "http://weixin.qq.com/download ",
  56. 'Topcolor' => "# FF0000 ",
  57. 'Data' => array (
  58. 'Name' => array ('value' => urlencode ($ name), 'color' => "# 00008B"), // name of the parameter passed by the function
  59. 'Zu' => array ('value' => urlencode ($ zu), 'color' => '# 00008B'), // zu passed by the function
  60. 'Time' => array ('value' => urlencode ($ times), 'color' => '# hour 8B'), // time
  61. 'Remain' => array ('value' => urlencode ($ remain), 'color' => '# comment 8B'), // ramain passed by the function
  62. )
  63. );
  64. $ Json_template = json_encode ($ template );
  65. $ Url = "https://api.weixin.qq.com/cgi-bin/message/template/send? Access_token = ". $ ACCESS_TOKEN;
  66. $ Res = http_request ($ url, urldecode ($ json_template ));
  67. If ($ res [errcode] = 0) echo 'message sent successfully! ';
  68. }

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.

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.