tourze/wechat-helper 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

tourze/wechat-helper

Composer 安装命令:

composer require tourze/wechat-helper

包简介

微信消息加密解密和XML处理助手工具包

README 文档

README

English | 中文

Latest Version PHP Version Require License Total Downloads Coverage

A PHP library providing essential helper functions for WeChat development, including AES encryption/decryption and XML processing utilities with comprehensive security validation.

Features

  • AES encryption and decryption with 128/192/256-bit key support
  • WeChat message encryption/decryption for secure communication
  • WeChat Mini Program data decryption support
  • XML parsing with sanitization and CDATA handling
  • Array to XML conversion with nested structure support
  • Comprehensive exception handling for security validation
  • Compatible with PHP 8.1+ and modern WeChat APIs

Dependencies

This package requires the following PHP extensions:

  • ext-json - JSON processing
  • ext-libxml - XML library functionality
  • ext-openssl - OpenSSL encryption
  • ext-simplexml - Simple XML processing

Installation

Install the package via Composer:

composer require tourze/wechat-helper

Quick Start

AES Encryption

<?php

use Tourze\WechatHelper\AES;

// Encrypt data
$plaintext = 'Hello WeChat';
$key = 'your-16-24-32-byte-key';  // 16, 24, or 32 bytes
$iv = 'your-16-byte-iv--';        // 16 bytes
$encrypted = AES::encrypt($plaintext, $key, $iv);

// Decrypt data
$decrypted = AES::decrypt($encrypted, $key, $iv);
echo $decrypted; // Output: Hello WeChat

WeChat Message Encryption

<?php

use Tourze\WechatHelper\Encryptor;

$encryptor = new Encryptor('your-app-id', 'your-token', 'your-encoding-aes-key');

// Encrypt outgoing message
$message = '<xml><Content><![CDATA[Hello]]></Content></xml>';
$encrypted = $encryptor->encrypt($message, 'timestamp', 'nonce');

// Decrypt incoming message
$content = 'encrypted-xml-data';
$msgSignature = 'wechat-msg-signature';
$nonce = 'nonce';
$timestamp = 'timestamp';
$decrypted = $encryptor->decrypt($content, $msgSignature, $nonce, $timestamp);

// Decrypt WeChat Mini Program data
$sessionKey = 'your-session-key';
$encryptedData = 'encrypted-mini-program-data';
$iv = 'your-iv';
$userData = $encryptor->decryptData($sessionKey, $encryptedData, $iv);

XML Processing

<?php

use Tourze\WechatHelper\XML;

// Parse XML to array
$xml = '<xml><name><![CDATA[John]]></name><age>25</age></xml>';
$array = XML::parse($xml);
// Result: ['name' => 'John', 'age' => '25']

// Convert array to XML
$data = [
    'name' => 'John',
    'age' => 25,
    'items' => ['apple', 'banana']
];
$xml = XML::build($data, 'xml', 'item');
// Output: <xml><name><![CDATA[John]]></name><age>25</age>...</xml>

// Create CDATA section
$cdata = XML::cdata('Special <characters> & symbols');
// Output: <![CDATA[Special <characters> & symbols]]>

Usage

Error Handling

All encryption/decryption operations include comprehensive error handling:

<?php

use Tourze\WechatHelper\Encryptor;
use Tourze\WechatHelper\Exception\InvalidSignatureException;
use Tourze\WechatHelper\Exception\DecryptException;

try {
    $encryptor = new Encryptor($appId, $token, $encodingAesKey);
    $result = $encryptor->decrypt($data, $msgSignature, $nonce, $timestamp);
} catch (InvalidSignatureException $e) {
    // Handle signature validation failure
    echo 'Invalid signature: ' . $e->getMessage();
} catch (DecryptException $e) {
    // Handle decryption failure
    echo 'Decryption failed: ' . $e->getMessage();
}

Advanced Usage

Custom XML Root and Item Names

<?php

use Tourze\WechatHelper\XML;

$data = ['item1', 'item2', 'item3'];
$xml = XML::build($data, 'root', 'element', 'id="list"', 'index');
// Output: <root id="list"><element index="0">...</element>...</root>

XML Sanitization

The XML parser automatically sanitizes input to remove invalid characters:

<?php

use Tourze\WechatHelper\XML;

$invalidXml = "<xml>Invalid\x00characters</xml>";
$cleanXml = XML::sanitize($invalidXml);
// Removes null bytes and other invalid XML characters

Testing

Run the test suite:

# Run all tests
./vendor/bin/phpunit packages/wechat-helper/tests

# Run tests with verbose output
./vendor/bin/phpunit packages/wechat-helper/tests --testdox

# Check test coverage (requires Xdebug)
./vendor/bin/phpunit packages/wechat-helper/tests --coverage-text

This package includes comprehensive tests with 61 test cases covering all major functionality and edge cases.

Contributing

We welcome contributions! Please follow these guidelines:

  1. Fork the repository
  2. Create a feature branch
  3. Add tests for new functionality
  4. Ensure all tests pass
  5. Submit a pull request

License

This project is licensed under the MIT License - see the LICENSE file for details.

Credits

Based on the original work by overtrue in the overtrue/wechat project.

tourze/wechat-helper 适用场景与选型建议

tourze/wechat-helper 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.88k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 05 月 01 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 tourze/wechat-helper 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 tourze/wechat-helper 我们能提供哪些服务?
定制开发 / 二次开发

基于 tourze/wechat-helper 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 3.88k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 3
  • 依赖项目数: 4
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-05-01