bangbangda/wecomarchive
Composer 安装命令:
pie install bangbangda/wecomarchive
包简介
PHP extension for WeCom (WeChat Work) Chat Archive functionality. Provides object-oriented interface to fetch chat messages, decrypt content, and download media files.
README 文档
README
English | 简体中文
📖 中国用户请查看 中文文档 →
PHP extension for WeCom (WeChat Work) Chat Archive functionality.
Install with PIE:
pie install bangbangda/wecomarchive
Features
- Automatic SDK Download: WeCom SDK is automatically downloaded during installation
- Object-Oriented Interface: Clean, modern PHP API for chat archive operations
- Full Functionality: Fetch messages, decrypt content, download media files
- Flexible Configuration: Support for custom SDK paths and proxy settings
Requirements
- PHP >= 8.0
- Linux (x86_64 or ARM64)
- OpenSSL >= 1.1.0
Installation
Method 1: PIE (Recommended)
PIE is the modern way to install PHP extensions.
Basic Installation (Automatic):
# Install PIE if you haven't already composer global require php/pie # Install the extension (SDK will be downloaded automatically) pie install bangbangda/wecomarchive
The WeCom SDK will be automatically downloaded during installation to /usr/local/lib/.
Advanced Configuration:
If you need to customize the SDK path or the automatic download fails:
# Specify custom SDK library path pie install bangbangda/wecomarchive --with-wecomarchive-sdk-path=/custom/lib/path # Then manually download SDK to your custom path vendor/bin/download-sdk.sh --path /custom/lib/path
Note: If automatic download fails due to permission issues, you may need to run:
# Download SDK manually with sudo
sudo ./scripts/download-sdk.sh
Method 2: Manual Installation
- Download and extract the source:
git clone https://github.com/bangbangda/wecomarchive.git
cd wecomarchive
- Build the extension (SDK will be downloaded automatically during configure):
phpize ./configure make sudo make install
The WeCom SDK will be automatically downloaded to /usr/local/lib/ during the ./configure step.
If you want to use a custom SDK path:
phpize ./configure --with-wecomarchive-sdk-path=/custom/lib/path make sudo make install
If automatic download fails, you can manually download the SDK first:
chmod +x scripts/download-sdk.sh
./scripts/download-sdk.sh
# Or with custom path
./scripts/download-sdk.sh --path /custom/path
- Enable the extension in php.ini:
extension=wecomarchive.so wecomarchive.sdk_lib_path=/usr/local/lib/libWeWorkFinanceSdk_C.so
Configuration
| INI Setting | Default | Description |
|---|---|---|
wecomarchive.sdk_lib_path |
/usr/local/lib/libWeWorkFinanceSdk_C.so |
Path to the WeCom SDK library |
Usage
Basic Example
<?php // Initialize with your credentials. private_key accepts either PEM content or a file path // (auto-detected — values starting with "-----BEGIN " are treated as PEM content). $archive = new WeComArchive([ 'corpid' => 'your_corp_id', 'secret' => 'your_secret', 'private_key' => '/path/to/private.pem', // or raw PEM string ]); // Fetch chat data $response = $archive->getChatData(seq: 0, limit: 100); $data = json_decode($response, true); if ($data['errcode'] === 0) { foreach ($data['chatdata'] as $chat) { // Decrypt message $message = $archive->decryptData( $chat['encrypt_random_key'], $chat['encrypt_chat_msg'] ); $msgData = json_decode($message, true); print_r($msgData); } }
Multi-version Private Keys (Recommended)
WeCom supports rotating the chat-archive private key. Each chat item carries a publickey_ver
field. Configure private_keys and use decryptChatItem() to let the extension auto-select
the correct private key for each item:
<?php $archive = new WeComArchive([ 'corpid' => 'your_corp_id', 'secret' => 'your_secret', // [publickey_ver => PEM content or file path] 'private_keys' => [ 1 => '/path/to/key_v1.pem', 2 => '/path/to/key_v2.pem', 3 => "-----BEGIN PRIVATE KEY-----\n...", ], ]); $response = $archive->getChatData(0, 100); $data = json_decode($response, true); foreach ($data['chatdata'] as $chat) { // Pass the whole chat item — extension picks the key by publickey_ver automatically. $message = $archive->decryptChatItem($chat); $msgData = json_decode($message, true); print_r($msgData); }
If publickey_ver cannot be matched in private_keys, a clear exception is thrown
naming the missing version.
Download Media Files
<?php // Get media file (image, video, file, etc.) $mediaContent = $archive->getMediaData($sdkFileId, [ 'timeout' => 30, ]); // Save to file file_put_contents('/path/to/output.jpg', $mediaContent);
Using Proxy
<?php $archive = new WeComArchive([ 'corpid' => 'your_corp_id', 'secret' => 'your_secret', 'private_key' => file_get_contents('/path/to/private.pem'), ]); // With proxy $response = $archive->getChatData(0, 100, [ 'proxy' => 'http://proxy.example.com:8080', 'passwd' => 'user:password', 'timeout' => 10, ]);
Custom SDK Library Path
<?php $archive = new WeComArchive([ 'corpid' => 'your_corp_id', 'secret' => 'your_secret', 'lib_path' => '/custom/path/to/libWeWorkFinanceSdk_C.so', ]);
API Reference
WeComArchive Class
Constructor
public function __construct(array $options)
Options:
corpid(required): Your WeCom Corp IDsecret(required): Chat archive secretprivate_key(optional): A single RSA private key. Accepts either PEM content or a file path (auto-detected). Used bydecryptData()private_keys(optional): Multi-version key map[publickey_ver => PEM-or-path]. Used bydecryptChatItem()for auto-selectionlib_path(optional): Custom path to SDK library
Detection rule: a value is treated as raw PEM content if it spans multiple lines (PEM bodies always contain newlines, file paths do not) or starts with
-----BEGIN. Single-line values without the PEM header are treated as a file path. This tolerates PEM exports that have leading metadata (e.g.Bag Attributeslines fromopenssl pkcs12).private_keyandprivate_keysmay both be set — the former acts as a fallback fordecryptChatItem().
getChatData
public function getChatData(int $seq = 0, int $limit = 100, array $options = []): string
Fetch chat messages.
Parameters:
$seq: Starting sequence number (0 for first fetch)$limit: Maximum messages to fetch (1-1000)$options: Optional settings (proxy, passwd, timeout)
Returns: JSON string with chat data
decryptData
public function decryptData(string $encryptRandomKey, string $encryptChatMsg): string
Decrypt a chat message using the private_key provided at construction time.
Parameters:
$encryptRandomKey: Theencrypt_random_keyfrom chat data$encryptChatMsg: Theencrypt_chat_msgfrom chat data
Returns: Decrypted message as JSON string
Throws: WECOM_ERR_PRIKEY if no private_key was configured.
decryptChatItem
public function decryptChatItem(array $chatItem): string
Decrypt one chatdata item, auto-selecting the private key from private_keys by its publickey_ver.
Parameters:
$chatItem: One element from thechatdataarray returned bygetChatData(). Must containencrypt_random_keyandencrypt_chat_msg; ifpublickey_veris present it is used to pick the matching key fromprivate_keys
Returns: Decrypted message as JSON string
Throws:
WECOM_ERR_PRIKEY— no key configured at all (neitherprivate_keysnorprivate_key)WECOM_ERR_PRIKEY—publickey_vernot found inprivate_keys(error message names the missing version)WECOM_ERR_PARAM— chat item is missing required fields
getMediaData
public function getMediaData(string $sdkFileId, array $options = []): string
Download media file content.
Parameters:
$sdkFileId: Thesdkfileidfrom message$options: Optional settings (proxy, passwd, timeout)
Returns: Binary content of the media file
getSdkVersion
public static function getSdkVersion(): string
Get the SDK version.
Error Codes
| Code | Constant | Description |
|---|---|---|
| 10000 | WECOM_ERR_PARAM |
Parameter error |
| 10001 | WECOM_ERR_NETWORK |
Network error |
| 10002 | WECOM_ERR_PARSE |
Data parse failed |
| 10003 | WECOM_ERR_SYSTEM |
System error |
| 10004 | WECOM_ERR_ENCRYPT |
Encryption failed |
| 10005 | WECOM_ERR_FILEID |
Invalid file ID |
| 10006 | WECOM_ERR_DECRYPT |
Decryption failed |
| 10007 | WECOM_ERR_PRIKEY |
Private key not found |
| 10008 | WECOM_ERR_ENCKEY |
Encrypt key parse error |
| 10009 | WECOM_ERR_IP |
IP not allowed |
| 10010 | WECOM_ERR_EXPIRED |
Data expired |
| 10011 | WECOM_ERR_CERT |
Certificate error |
License
PHP License 3.01
bangbangda/wecomarchive 适用场景与选型建议
bangbangda/wecomarchive 是一款 基于 C 开发的 Composer 扩展包,目前已累计 6 次下载、GitHub Stars 达 1, 最近一次更新时间为 2026 年 01 月 08 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「extension」 「chat」 「archive」 「enterprise」 「wechat」 「wework」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 bangbangda/wecomarchive 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 bangbangda/wecomarchive 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 bangbangda/wecomarchive 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A custom URL rule class for Yii 2 which allows to create translated URL rules
This simple PHP class allows you to easily generate Smartsupp.com JS chat code.
Client for the REST API plugin of the OpenFire Server
The Yii2 extension uses jQuery jquery.carousel-1.1.min.js and makes image carousel from php array of structure defined.
TYPO3 CMS extension to create gallery content element with preset crop ratios and pagination
UI Kit 3 Extension for Yii2
统计信息
- 总下载量: 6
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 39
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: PHP-3.01
- 更新时间: 2026-01-08