This article brings you the content is about PHP implementation of small programs to send template messages (code), there is a certain reference value, the need for friends can refer to, I hope to help you.
The address is:
Https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=ACCESS_TOKEN
The relevant parameters are
Parameters |
must fill | in
Description |
Touser |
Is |
OpenID for recipient (user) |
template_id |
Is |
ID of the template message to be issued |
Page |
Whether |
Click the jump page after the template card, only the page within this applet. Support with parameters, (example Index?foo=bar). This field does not fill the template without a jump. |
form_id |
Is |
Form submission scenario, the FormId for the submit event; Payment scenario, the prepay_id for this payment |
Data |
Is |
Template content, not fill out the empty template issued |
Color |
Whether |
Template content font color, do not fill the default black "obsolete" |
Emphasis_keyword |
Whether |
The template needs to enlarge the keyword, do not fill the default no magnification |
The basic parameters and address is the above test, some people will be wondering where to get the following form_id,openid,tmeplate_id first simple
The form_id can be provided by the front end, and the front end can submit a form on each button to get form_id to the backend, while the backend gets the relevant OpenID. It is recommended here that the front end is as much as possible to the backend, that is, each button is provided to the backend a id,form_id is valid for 7 days, regardless of whether the operator is used, the quantity is much better than the quantity is few.
TEMPLATE_ID is the template ID, you can log in to the applet background to get to the template message.
When we know these parameters, the developer would like to test immediately, and then let the front-end provide form_id, here is the next 2 pits
The first pit: computer-acquired form_id is not available
The second pit: form_id is to get the real mobile phone, but the real machine at the same time also if the line of the project, not online local testing will prompt to verify the form_id, is not to explain this point of attention, that is, it is necessary to audit the successful release of online testing.
Next, let's say back-end code
<?php//Get Accesstoken Public function Getaccesstoken ($appid, secret) {$url = "Https://api.weixin.qq.com/cgi-bin /token?grant_type=client_credential&appid={$appid}&secret={$secret} "; $res = $this->curl_get ($url); $res = Json_decode ($res, 1); return $res [' Access_token ']; }//get template message content body//Because it is a test so write dead, you can get Public function getmsg ($openid, $template _id, $form _id, $emphasis _keyword= ' Keyword1 ') {$data [' Data ']= [' keyword1 ' =>[' value ' = ' test1 ', ' color ' = '], ' keyword2 ' =>[' value ' = ' Test2 ', ' color ' = '], ' keyword3 ' =>[' value ' = ' test1 ', ' color ' = ']];//content body $data [' touser '] = $openid;// The user's OpenID $data [' template_id '] = $template _id;//Gets the template ID from the background $data [' form_id '] = $form _id;//front end provided to the backend form_id $data [' page '] = ' pages/index/index ';//Small program jump page $data [' emphasis_keyword '] = $emphasis _keyword;//Select enlarged font re Turn $data; Public function Send ($appid, Secret, $openid, $template _id, $form _id) {$access _token = $this->getaccesstoken ($appid, secret); $send _url = ' https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send? Access_token= '. $access _token; $data = $this->getmsg ($openid, $template _id, $form _id); $str = $this->curl_post ($send _url,json_encode ($data)); $str = Json_decode ($str, 1); return $str; Public Function Curl_post ($url, $fields, $data _type= ' text ') {$cl = Curl_init (); if (Stripos ($url, ' https://')!== FALSE) {curl_setopt ($CL, Curlopt_ssl_verifypeer, false); curl_setopt ($CL, Curlopt_ssl_verifyhost, FALSE); curl_setopt ($CL, curlopt_sslversion, 1); } curl_setopt ($cl, Curlopt_url, $url); curl_setopt ($CL, Curlopt_returntransfer, 1); curl_setopt ($CL, Curlopt_post, true); curl_setopt ($CL, Curlopt_postfields, $fields); $content = curl_exec ($CL); $status = Curl_getinfo ($CL); Curl_close ($CL); if (ISSet ($status [' Http_code ']) && $status [' http_code '] = = () {if ($data _type = = ' json ') { $content = Json_decode ($content); } return $content; } else {return FALSE; }} Public Function Curl_get ($url, $data _type= ' text ') {$cl = Curl_init (); if (Stripos ($url, ' https://')!== FALSE) {curl_setopt ($CL, Curlopt_ssl_verifypeer, false); curl_setopt ($CL, Curlopt_ssl_verifyhost, FALSE); curl_setopt ($CL, curlopt_sslversion, 1); } curl_setopt ($cl, Curlopt_url, $url); curl_setopt ($CL, Curlopt_returntransfer, 1); $content = curl_exec ($CL); $status = Curl_getinfo ($CL); Curl_close ($CL); if (Isset ($status [' Http_code ']) && $status [' http_code '] = = +) {if ($data _type = = ' json ') { $content = Json_decode ($content); } return $content; } else {return FALSE; }} punblic function index () {$appid = ' xxx ';//applet AppID $openid = ' xxx ';//Receive User's OpenID $template _id = ' xxx ';//the template ID obtained from the background $form _id = ' xxx ';//Formid within seven days $data = $this->send ($appid , Secret, $openid, $template _id, $form _id); Var_dump ($data);//Print test results}
The above is the code to send the template message, in fact, as long as a few corresponding parameters can be noted that the relevant pits can be successfully tested sent