Backend Development 4 min read

Integrating PHP with WeChat Work: Step-by-Step Guide

This guide explains how to integrate PHP with WeChat Work by registering a corporate account, creating an application, obtaining credentials, using PHP’s HTTP requests to acquire access tokens, and sending messages or retrieving user data, including a complete code example.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Integrating PHP with WeChat Work: Step-by-Step Guide

Integrating PHP with WeChat Work enables enterprises to automate workflows such as sending messages and retrieving employee information.

Register a corporate account on the WeChat Work website and enable developer mode to obtain necessary credentials.

Create an enterprise application in the admin console, providing name, logo, callback URL, and receive AgentId, CorpId, and Secret.

Use PHP’s HTTP client (e.g., cURL) to call the WeChat Work API for functions like messaging and member queries.

Obtain an access token by requesting $apiUrl/gettoken?corpid=...&corpsecret=... with CorpId and Secret.

Send messages by posting to $apiUrl/message/send?access_token=... with the token and AgentId.

Retrieve detailed member information via the member‑info endpoint using the same token and AgentId.

Consult the official API documentation for additional interfaces and customize calls as needed.

WeChat Work also supports event callbacks; configure a callback URL in the admin console and write PHP code to process incoming notifications.

During integration, handle errors, validate request legitimacy, and protect sensitive data.

Below is a complete PHP example that obtains an access token and sends a text message.

<code><?php
// 企业微信API接口地址
$apiUrl = 'https://qyapi.weixin.qq.com/cgi-bin';

// 应用凭证
$corpId = 'YOUR_CORP_ID'; // 替换为您的企业CorpId
$corpSecret = 'YOUR_CORP_SECRET'; // 替换为您的应用Secret

// 获取访问令牌
$accessTokenUrl = $apiUrl . '/gettoken?corpid=' . $corpId . '&corpsecret=' . $corpSecret;
$response = json_decode(file_get_contents($accessTokenUrl));
$accessToken = $response->access_token;

// 发送消息
$messageUrl = $apiUrl . '/message/send?access_token=' . $accessToken;

$data = array(
    'touser' => 'USER_ID', // 替换为要发送消息的用户ID
    'msgtype' => 'text',
    'agentid' => 'YOUR_AGENT_ID', // 替换为您的应用AgentId
    'text' => array('content' => 'Hello, World!'), // 替换为您要发送的消息内容
);

$options = array(
    'http' => array(
        'header' => "Content-Type: application/json\r\n",
        'method' => 'POST',
        'content' => json_encode($data),
    ),
);
$context = stream_context_create($options);
$result = file_get_contents($messageUrl, false, $context);

// 处理发送结果
$response = json_decode($result);
if ($response->errcode == 0) {
    echo '消息发送成功';
} else {
    echo '消息发送失败:' . $response->errmsg;
}
?>
</code>
Backend integrationMessagingAPIcallbackWeChat Workaccess token
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.