Current Location: Home> Latest Articles> How to Implement Subscription Feature for WeChat Official Accounts using PHP

How to Implement Subscription Feature for WeChat Official Accounts using PHP

M66 2025-06-16

How to Implement WeChat Official Account Subscription Feature using PHP

With the rapid development of mobile internet, WeChat official accounts have become an important platform for businesses and individuals to disseminate information. The subscription feature is one of the core functions of WeChat official accounts, allowing users to receive the latest messages and updates after following the account.

This article will guide you on how to implement the subscription feature for WeChat official accounts using PHP, with specific code examples provided.

Getting User's openid

Before developing the subscription feature, the first step is to obtain the user's openid. After a user follows the official account, the official account will push user information to the developer's server. In PHP, you can parse the incoming XML data to get the user's openid.

Here is an example code for obtaining openid:

<?php
$xml = file_get_contents('php://input');
$data = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
$openid = $data->FromUserName;
?>
    

Storing User Information

Once you obtain the user's openid, you need to store the user information in a database, so that you can send messages based on the openid later.

We can use MySQL to store the user information. Below is the code to create a users table and store the user's openid and subscription time:

<?php
$servername = "localhost";
$username = "yourusername";
$password = "yourpassword";
$dbname = "yourdbname";

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "INSERT INTO users (openid, subscribe_time) VALUES ('$openid', NOW())";

if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?>
    

Sending Subscription Messages

When you need to send subscription messages to users, you can use the WeChat API. In PHP, you can use the curl function to send HTTP requests.

First, you need to get the content for the subscription message and package it into XML format. Then, you use curl to send a POST request with the XML data to the WeChat server.

Here is an example code to send subscription messages:

<?php
$access_token = "youraccess_token";
$openid = "useropenid";
$template_id = "yourtemplateid";
$content = "yourcontent";

$url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $access_token;

$data = array(
    "touser" => $openid,
    "template_id" => $template_id,
    "data" => array(
        "content" => array(
            "value" => $content
        )
    )
);

$data_json = json_encode($data);

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Content-Length: ' . strlen($data_json)
));

$response = curl_exec($ch);

if (curl_errno($ch)) {
    echo 'Curl error: ' . curl_error($ch);
}

curl_close($ch);

echo $response;
?>
    

These are the specific code examples for implementing the WeChat subscription feature using PHP. By obtaining the user's openid, storing user information, and sending subscription messages, developers can implement a complete subscription feature and send the latest messages to users. Additionally, developers can further enhance and optimize this functionality according to their specific needs.