PHP Code Collation _php tutorial for WeChat push template messages

Source: Internet
Author: User
Tags openid vars

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

 
  
  
  1. {First.} DATA}}
  2. The person who was torn: {{name. DATA}}
  3. Category of the torn man: {{zu. DATA}}
  4. 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 ()

  
 
  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 = "" ; //fill in the Backstage AppID
  16. $appsecret = "" ; //fill in the Backstage 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= ="") //Database query no results get Access_token and deposit
  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. $sql 1 = "INSERT into ' tokentime ' (' id ', ' access_token ', ' time ') VALUES (' $appid ', ' $ACCESS _token ', ' $time ')" ;
  29. $query 1 =mysql_query ($sql 1);
  30. }
  31. Else
  32. { $time _b=$rk[' time ']; //Last Saved 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. $sql 2 = "UPDATE tokentime SET access_token= ' $ACCESS _token ', time= ' $time ' WHERE id= ' $appid '" ;
  41. $query 2 =mysql_query ($sql 2);
  42. }
  43. Else
  44. {
  45. $ACCESS _token = $rk [' Access_token '];
  46. }
  47. }
  48. //Template message
  49. $times = Date (' m ' D Day h:i:s ', Time ());
  50. $template = Array (
  51. ' Touser ' = $openid ,
  52. ' template_id ' = "_0DQERSIQPZAB4VJQJJOIPRXZHCVOOFT_390VDHHHVW" , //ID of the template
  53. ' URL ' = "Http://weixin.qq.com/download" ,
  54. ' Topcolor ' = "#FF0000" ,
  55. ' Data ' = Array (
  56. ' name ' = Array (' value '=>urlencode ($name),' color '= "#00008B" ), //function to pass over the name
  57. ' zu ' = Array (' value '=>urlencode ($zu),' color '= >' #00008B '), //function to pass over the Zu
  58. ' Time ' = Array (' value '=>urlencode ($times),' color '= ' #00008B ' ), //Time
  59. ' remain ' = Array (' value '=>urlencode ($remain),' color ' =' #00008B '),//function to pass over the Ramain
  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. }

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 ...

  • 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.