php WeChat official account sends template messages

Posted by IgglePiggle on Thu, 30 Apr 2020 20:56:11 +0200

Let's talk about the implementation process of sending WeChat template messages to the official account in the development project (I use Thinkphp5.0).

First look at the effect, as shown in the figure:

It is similar to this. Let's talk about the implementation process:

Step one: WeChat official account application template message permissions:

Apply now:

The application process is over. After the application is submitted and passed, you can see the template message list in the template library:

Which template do you want to click to add

After adding a template, it will be stored in my template library. Here are some things I need to add:

Click to view the details of the template. You can see the id of the template and the name of each content parameter. The content structure of different template messages is different. These IDs and field names are used in the program:

Step 2: program implementation template message SDK:

Similarly, the template message is sent using access token, so you need to get the token.

I put the class file of template message under extend, as shown in the following figure:

The code screenshot is as follows:

Class files can be used in this way, and then they can be introduced directly.

Step 3: build the template message:

I have written all the template messages that need to be used into one method and put them in the public class file. Please refer to the following:

 1 //Order success notification template
 2 /*
 3  * openid:Wechat unique identification
 4  * orderSn:order number
 5  * goods_name:Commodity name
 6  * goods_num:Quantity of goods
 7  * allMoney:Total price
 8  */
 9 function orderSuccess($openid=null,$orderSn=null,$goods_name=null,$goods_num=null,$allMoney=null){
10     $template = array(
11         "touser" => "$openid",
12         "template_id" => "gApc7CzZSiJOZ7OeoXKK8APmz-dcuQuwfBTzPblEWv4",
13         'url' => 'Jump address',
14         'topcolor' => '#ccc',
15         'data' => array('first'   => array('value' =>urlencode('Dear, your order has been created successfully, we will prepare the goods for you immediately, and arrange a special person to deliver the goods to your home free of charge in the first time! The order details are as follows'),
16                                            'color' =>"#743A3A",
17         ),
18             'keyword1' => array('value' =>urlencode($orderSn),
19                                 'color' =>'#FF0000'
20             ),
21             'keyword2' => array('value' =>urlencode($goods_name),
22                                 'color' =>'#FF0000'
23             ),
24             'keyword3' => array('value' =>urlencode($goods_num),
25                                 'color' =>'#FF0000'
26             ),
27             'keyword4' => array('value' =>urlencode($allMoney),
28                                 'color' =>'#FF0000'
29             ),
30             'keyword5' => array('value' =>urlencode('Online payment'),
31                                 'color' =>'#FF0000'
32             ),
33             'remark'   => array('value' =>urlencode('If you have any questions about the above information, please reply to your questions directly on the platform, or call the customer service number 4001021789. Thank you again for your patronage!'),
34                                 'color' =>'#FF0000'
35             ),
36         )
37     );
38     return $template;
39 }

This is just one of the template messages I wrote about the success of placing an order, which is put in the common file, so it is basically completed:

The following template message is sent to WeChat official account.

1  
2                     //Construct message template
3                     $template =orderSuccess($openid,$reoderSn,$goodsNames,$sum,$money);
4                     //Import template message class
5                     Loader::import('org\WxMessage', EXTEND_PATH);
6                     //Instantiate message class
7                     $message = new \WxMessage();
8                     //send message
9                     $message->send_template_message(urldecode(json_encode($template)));

Use the loader to import the message file under extend. After sending the message, you will get the picture content at the beginning.

It's very simple. You can refer to it if it's useful.

This is the original content. In order to respect the work of others, please indicate the address of this article:

http://www.cnblogs.com/luokakale/p/8143684.html

Topics: PHP SDK