PHP code arrangement for pushing template messages on WeChat _ PHP Tutorial

Source: Internet
Author: User
PHP code for pushing template messages. PHP code for pushing template messages has recently worked on a system that needs to push messages, so we have studied how to push template messages. The authenticated code is used to sort out the PHP code of the test push template message.

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

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.

Vivo has recently worked on a system that needs to push messages, and has studied the push of template messages. Because of the authenticated number, use the test...

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.