WeChat official account template message sending php code example

Source: Internet
Author: User
Tags php code examples
This article mainly introduces the php code example for mass messaging in the public account template, which has some reference value, interested friends can refer to this article for details about the public account template message sending php code example, which has some reference value. interested friends can refer

Template messages can only be sent to one person. if you want to send messages in a group, you need to use php loop to send messages in sequence.

Note: If the template message is occasionally unavailable and unstable when it is sent, your access_token token update cache may be delayed and expired. you can view it according to the log file. we recommend that you update it in 300 seconds. otherwise, it will be annoying.

The template id must be set in the public account to get it.

 
 GetList ($ SQL); $ postUrl =" https://api.weixin.qq.com/cgi-bin/message/template/send?access_token= $ Access_token "; foreach ($ row as $ k => $ v) {$ uid = $ v ['uid']; $ member_band = $ thisdb-> GetOne ("SELECT B _code, B _data FROM '@ # _ member_band' WHERE 'B _ uid' =' $ uid' and B _data! = ''"); // If (empty ($ member_band) {return false;} // return $ row; if (! Empty ($ member_band ['B _ data']) {$ url = WEB_PATH. "/mobile/user/buyDetail /". $ shopid; $ mobile = $ v ['company _ money']; $ data = array ("touser" => $ member_band ['B _ Code'], // send only one person at a time "template_id" => $ template_id, "url" => $ url, // callback "data" => array ('first' => array ("value" => "Hello ,". $ v ['username']. ", your Board has been set up successfully. "," Color "=>" #888 ",)," keyword1 "=> array (" value "=> $ v ['shopname'], "color" => "#888",), // echo date ('Y-m-d H: I: S ', $ itemlist [0] ['Q _ end_time ']) "keyword2" => array ("value" => date ('Y-m-d H: I: S ', $ v ['Time']), "color" => "#888",), "keyword3" => array ("value" => "20 minutes ", "color" => "#888",), "keyword4" => array ("value" => round ($ v ['moneycount']). "Beans", "color" => "#888",), "keyword5" => array ("value" => "TBD ", "color" => "#888",), "remark" => array ("value" => "Click to view details ", "color" => "#054ff7",); https_request ($ postUrl, json_encode ($ data), $ mobile); // The content of the delivery notification. sent to the server} // The string combination of the shipping prompt is sent successfully. prepare to send .}}} // The shipping preparation function getAccessToken () {// access_token should be stored and updated globally. the following code is written to the file for example $ path = G_CACHES. 'access _ token. json '; $ data = json_decode (file_get_contents ($ path), true); // print_r ($ data); exit; if ($ data ['expire _ time'] <time () {// if the expiration time is earlier than the current time, it indicates that it expires, you need to update $ appid = $ data ['appid ']; $ appsecret = $ data ['appsecret']; $ url =" https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid= $ Appid & secret = $ appsecret "; $ json = getCurl ($ url); $ data = json_decode ($ json, true); // print_r ($ res); exit; $ access_token = $ data ['Access _ token']; if ($ access_token) {$ thisdb = System: load_sys_class ("model "); $ wechat = $ thisdb-> GetOne ("select * from '@ # _ wechat_config' where id = 1 "); // Obtain appid $ data ['expire _ time'] = time () + 300; // update once every 5 minutes. you can request more than 500 requests per day. // It is best not to set the value of 300 to be too large. otherwise, the system will often prompt that the token has expired and cannot send messages. official statement 2 Expired in hours. it is actually a lie. far from reaching. $ data ['Access _ token'] = $ access_token; $ data ['Template _ id'] = $ wechat ['Template _ id']; $ data ['appid '] = $ wechat ['appid']; $ data ['appsecret'] = $ wechat ['appsecret']; $ fp = fopen ($ path, "w"); fwrite ($ fp, json_encode ($ data); fclose ($ fp) ;}return $ data ;} function https_request ($ url, $ data = null, $ mobile = '') {$ curl = curl_init (); // initialize a CURL session curl_setopt ($ curl, CURLOPT_URL, $ url ); // This is what you want URL retrieved using PHP. You can also set this option curl_setopt ($ curl, CURLOPT_SSL_VERIFYPEER, FALSE) when using the curl_init () function initialization; curl_setopt ($ curl, CURLOPT_SSL_VERIFYHOST, FALSE); if (! Empty ($ data) {curl_setopt ($ curl, CURLOPT_POST, 1); curl_setopt ($ curl, CURLOPT_POSTFIELDS, $ data ); // transmits a string of all data for the HTTP "POST" operation.} Curl_setopt ($ curl, CURLOPT_RETURNTRANSFER, 1); // The information obtained by curl_exec () is returned in the form of a file stream instead of a direct output. $ Output = curl_exec ($ curl); if (! Strstr ($ output, ": 0") // if the receipt after sending does not contain: 0, it indicates that the message has not been sent successfully. therefore, we need to put the unsuccessful ones in the log. it is convenient to find the cause. {$ path1 = G_CACHES. 'access _ token2.json'; // access_token2.json is the log storage directory. modify the directory accordingly. file_put_contents ($ path1, $ output, FILE_APPEND); // FILE_APPEND indicates saving logs by append.} if (strstr ($ output, "40003") & $ mobile) // if it is 40003, the customer did not pay attention to us and sent a message to remind him. {_ sendmobile ($ mobile, 'follow our public account. otherwise, you cannot receive the appointment card notification. thank you. '); //} Curl_close ($ curl); return json_decode ($ output);} // Determine whether the user name is a common administrator function is_ugly ($ name) {if (strstr ($ name, "gly") {return true;} else {return false ;}}

The above is all the content of this article. I hope it will help you learn and support PHP.

For more articles on common account template message sending php code examples, refer to PHP Chinese network!

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.