Current Location: Home> Latest Articles> PHP WeChat Official Account Development: How to Implement WeChat Message Notification Feature

PHP WeChat Official Account Development: How to Implement WeChat Message Notification Feature

gitbox 2025-06-28

Overview

This article primarily introduces how to develop a WeChat Official Account using PHP to implement the function of sending WeChat message notifications to users. WeChat message notifications help users receive timely information. Before development, it's essential to understand the basic concepts of WeChat Official Accounts, including account types, permissions, and API calls.

Implementation of WeChat Message Notification Feature

Obtaining Access Token

Before starting the WeChat message notification development, you need to obtain the access_token. The access_token is a credential used to call WeChat APIs. With it, you can access various WeChat functions. You can obtain the access_token using the following API:


$appid = "Your AppID";
$appsecret = "Your AppSecret";
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appsecret";
$result = file_get_contents($url);
$jsonResult = json_decode($result);
$access_token = $jsonResult->access_token;

Sending Template Messages

Sending template messages is a common method for implementing WeChat message notifications. To send a template message, the following conditions must be met:

  • The WeChat Official Account must obtain user authorization to get the user's openid;
  • You need to create the template for the message;
  • You need to call the API to send the template message to the user.

After creating the template, you can send the template message to the user using the following code:


$openId = "User's openid";
$templateId = "Template ID";
$url = "Redirect URL";
$data = array(
    "first" => array(
        "value" => "This is a template message",
        "color" => "#173177"
    ),
    "keyword1" => array(
        "value" => "Keyword 1",
        "color" => "#173177"
    ),
    "keyword2" => array(
        "value" => "Keyword 2",
        "color" => "#173177"
    ),
    "keyword3" => array(
        "value" => "Keyword 3",
        "color" => "#173177"
    ),
);
$jsonData = json_encode($data);
$url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $access_token;
$postData = array(
    "touser" => $openId,
    "template_id" => $templateId,
    "url" => $url,
    "data" => $data
);
$postJson = json_encode($postData);
$result = http_request($url, $postJson);

function http_request($url, $data = null) {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    if (!empty($data)) {
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    }
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($curl);
    curl_close($curl);
    return $output;
}

Conclusion

This article explained the process of developing a WeChat Official Account using PHP to send WeChat message notifications to users. During development, it's important to comply with the rules set by the WeChat Developer Platform and protect user privacy.