Current Location: Home> Latest Articles> How to Use PHP to Implement WeChat Official Account News Message Push Function

How to Use PHP to Implement WeChat Official Account News Message Push Function

M66 2025-10-16

Implementing WeChat Official Account News Message Push with PHP

As WeChat Official Accounts become increasingly popular, more businesses and individuals want to use them to deliver information and promote products. One of the most effective methods is sending news-style messages. This article explains how to use PHP to implement the WeChat Official Account news message push function, from setup to actual sending.

Preparation

Before starting development, make sure the following requirements are met:

  • A verified WeChat Official Account, which you can register on the WeChat Official Platform.
  • A custom menu configured on the WeChat Official Platform with valid redirect links.
  • A working PHP development environment (e.g., Apache + PHP).

Getting the access_token

Before calling WeChat API interfaces, you must obtain an access_token, which serves as the credential for API calls. Below is the PHP code example for retrieving it:

function getAccessToken($appId, $appSecret) {
    $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appId."&secret=".$appSecret;
    $result = file_get_contents($url);
    $result = json_decode($result, true);
    return $result['access_token'];
}

$appId = "your_app_id";
$appSecret = "your_app_secret";
$accessToken = getAccessToken($appId, $appSecret);

Replace your_app_id and your_app_secret with the actual values from your WeChat Official Account.

Building the news message

Once you have the access_token, you can construct the news message. Each message can include a title, description, image URL, and a redirect link. Here’s an example:

$articles = array(
    array(
        'title' => "News Title 1",
        'description' => "News Description 1",
        'url' => "http://example.com/article1",
        'picurl' => "http://example.com/article1.jpg"
    ),
    array(
        'title' => "News Title 2",
        'description' => "News Description 2",
        'url' => "http://example.com/article2",
        'picurl' => "http://example.com/article2.jpg"
    ),
);

You can add more news items as needed to create a rich message layout.

Sending the news message

With the access_token and message data ready, you can send the news message using the WeChat mass messaging API. Below is an example in PHP:

function sendArticles($accessToken, $articles) {
    $url = "https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token=".$accessToken;
    $data = array(
        'touser' => "@all",
        'msgtype' => "news",
        'news' => array('articles' => $articles)
    );
    $jsonData = json_encode($data);

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($ch);
    curl_close($ch);
    return $response;
}

$response = sendArticles($accessToken, $articles);

Replace $accessToken with your actual access_token to send the message to all followers.

Conclusion

By following the steps above, you can implement the WeChat Official Account news message push function using PHP. Combining PHP with the WeChat API allows developers to automatically deliver rich content to users, improving efficiency and engagement for public account operations.

Be sure to comply with WeChat’s official interface rate limits and message sending policies to ensure smooth and stable operation.