Current Location: Home> Latest Articles> Complete Guide to Implementing WeCom Message Subscription Using PHP

Complete Guide to Implementing WeCom Message Subscription Using PHP

M66 2025-07-02

Introduction to WeCom Message Push

WeCom is a communication tool developed for enterprise teams. It provides a robust set of APIs that support internal messaging, system notifications, and workflow alerts. By integrating WeCom’s message subscription functionality, businesses can push updates and notifications directly to employees or departments.

Preparation: Create a WeCom App

To begin, log in to the WeCom admin portal and create a custom application. Take note of the following required credentials:

  • Corp ID (corpId)
  • App ID (agentId)
  • App Secret

Install Required PHP Libraries

To simplify HTTP requests and token generation, use Composer to install the following packages:

  • guzzlehttp/guzzle - For sending HTTP requests
  • firebase/php-jwt - For generating and decoding JWT tokens

Understand WeCom Message Payload Format

The WeCom API requires a specific structure for message payloads. Below is a sample JSON payload:


{
  "touser": "UserID1|UserID2",
  "agentid": 1,
  "msgtype": "text",
  "text": {
    "content": "Your message content here"
  },
  "safe": 0
}

Explanation of key fields:

  • touser: Recipients of the message, separated by a pipe (|)
  • agentid: ID of the sending application
  • msgtype: Type of message (e.g., "text")
  • text: Message body
  • safe: Indicates whether the message is confidential (1) or not (0)

PHP Code to Send Messages via WeCom API

The following PHP script demonstrates how to build and send a message to WeCom’s API:


require 'path/to/vendor/autoload.php';

use GuzzleHttp\Client;
use Firebase\JWT\JWT;

// WeCom app configuration
$corpId  = 'your_corp_id';
$agentId = 'your_agent_id';
$secret  = 'your_secret_key';

function sendMsg($touser, $content) {
    global $corpId, $agentId, $secret;

    $data = [
        'touser'  => $touser,
        'agentid' => $agentId,
        'msgtype' => 'text',
        'text'    => [ 'content' => $content ],
        'safe'    => 0
    ];

    // Generate JWT token
    $time = time();
    $payload = [
        'iat' => $time,
        'exp' => $time + 3600,
        'iss' => $corpId
    ];
    $token = JWT::encode($payload, $secret);

    // Send request
    $client = new Client(['base_uri' => 'https://qyapi.weixin.qq.com']);
    $response = $client->request('POST', '/cgi-bin/message/send', [
        'query' => ['access_token' => $token],
        'json'  => $data
    ]);

    $result = json_decode($response->getBody(), true);
    return $result['errcode'] === 0;
}

Trigger Message Sending in Business Logic

You can now use the above function in your application logic to send targeted messages:


$touser = 'UserID1';
$content = 'You have a new message. Please check.';
$result = sendMsg($touser, $content);

if ($result) {
    echo 'Message sent successfully';
} else {
    echo 'Failed to send message';
}

Conclusion and Best Practices

This tutorial showed how to implement a WeCom message subscription system using PHP. You can enhance it further by:

  • Adding error handling and logging
  • Supporting multiple message formats (e.g., images, cards)
  • Integrating with a database for dynamic message content
  • Refactoring into a reusable service or class

With this approach, enterprises can build an efficient and scalable internal messaging system to improve operational communication.