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.

<?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
",
        '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;
}
?>
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Backend IntegrationMessagingPHPAPIcallbackwechat-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

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.