WeChat template message push is a feature that allows sending predefined template messages from a WeChat Official Account to users. In PHP, this functionality can be implemented using the API provided by the WeChat Open Platform. This article will detail how to implement WeChat template message push with PHP code.
Before starting the implementation, we need to prepare the following two prerequisites:
First, you need to have a WeChat Official Account, and it should be registered and authenticated. If you don’t have one, you can register through the WeChat Official Platform.
Next, create an application on the WeChat Open Platform and obtain the corresponding AppID and AppSecret. These details are used to call the API and push messages.
Before calling the API, we need to obtain the access_token. The access_token is the credential for calling the WeChat API and needs to be refreshed every two hours.
// Configure according to your actual situation $appId = 'your_app_id'; $appSecret = 'your_app_secret'; // Send request to get access_token $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appId}&secret={$appSecret}"; $response = file_get_contents($url); $result = json_decode($response, true); if (isset($result['access_token'])) { $accessToken = $result['access_token']; } else { // Failed to get access_token, handle error logic }
Before sending the template message, you need to create a template in the WeChat Official Account backend. The template defines the content and style of the message.
$templateId = 'your_template_id'; // Configure according to your actual situation $openid = 'user_openid'; // User's openid // Template message content $data = array( 'first' => array( 'value' => 'You have a new message', 'color' => '#173177' ), 'keyword1' => array( 'value' => 'Template message title', 'color' => '#173177' ), 'keyword2' => array( 'value' => 'Template message content', 'color' => '#173177' ), 'remark' => array( 'value' => 'Please check it in time', 'color' => '#173177' ) ); // Assemble request data $messageData = array( 'touser' => $openid, 'template_id' => $templateId, 'data' => $data, ); // Send template message $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={$accessToken}"; $options = array( 'http' => array( 'header' => "Content-Type: application/json", 'method' => "POST", 'content' => json_encode($messageData) ) ); $context = stream_context_create($options); $response = file_get_contents($url, false, $context); $result = json_decode($response, true); if ($result['errcode'] == 0) { // Successfully sent, handle success logic } else { // Sending failed, handle failure logic }
Through the above steps, we can implement pushing WeChat template messages to users via PHP code. First, we need to get the access_token, then create the template message and send it to the specified user. It's important to customize the content of the template message based on actual needs.
I hope this article helps you implement WeChat template message pushing in PHP!