With the growing popularity of WeChat Official Accounts, more developers and businesses are exploring WeChat’s development features. One of the most commonly used features is template messages, which allow sending structured notifications to users. These messages are widely used in scenarios like order updates, notifications, and reminders. This article walks you through how to implement this functionality in PHP with practical code examples.
Before you can send a template message via PHP, make sure you meet the following prerequisites:
The access token is required for accessing the WeChat API and has a limited validity period. Here’s how you can fetch it using PHP:
function getAccessToken($appId, $appSecret) {
$apiUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $appId . "&secret=" . $appSecret;
$response = file_get_contents($apiUrl);
$result = json_decode($response, true);
// Check if token is valid
if (isset($result['access_token'])) {
return $result['access_token'];
} else {
// Handle error
return false;
}
}
$accessToken = getAccessToken($appId, $appSecret);
Once the access token is obtained, you can send a template message using the following function:
function sendTemplateMessage($accessToken, $openId, $templateId, $data) {
$apiUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $accessToken;
$postData = array(
'touser' => $openId,
'template_id' => $templateId,
'data' => $data
);
$jsonData = json_encode($postData);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apiUrl);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
// Sample template message data
$templateData = array(
'first' => array('value' => 'Hello', 'color' => '#173177'),
'keyword1' => array('value' => 'Template Message', 'color' => '#173177'),
'keyword2' => array('value' => '2020-01-01', 'color' => '#173177'),
'remark' => array('value' => 'Thank you for using our service', 'color' => '#173177')
);
$response = sendTemplateMessage($accessToken, $openId, $templateId, $templateData);
// Handle response
$result = json_decode($response, true);
if ($result['errcode'] == 0) {
echo "Template message sent successfully!";
} else {
echo "Failed to send message. Error: " . $result['errmsg'];
}
The sendTemplateMessage function accepts the access token, the user’s OpenID, the template ID, and the message data array. Each item in the data array corresponds to a placeholder defined in your WeChat template.
In this tutorial, we demonstrated how to implement template message functionality in a WeChat Official Account using PHP. You first need to acquire an access token, then use that token to send structured messages to users. With the provided code examples, developers can quickly integrate this feature into their applications and enhance user communication effectively.
This guide aims to help PHP developers streamline their workflow when working with the WeChat Official Account messaging API.