PHPackages                             techotaku/wechat-sdk - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. techotaku/wechat-sdk

AbandonedArchivedLibrary[API Development](/categories/api)

techotaku/wechat-sdk
====================

Wechat PHP SDK

1.0.1(12y ago)6712MITPHPPHP &gt;=5.3.0

Since Sep 3Pushed 12y ago1 watchersCompare

[ Source](https://github.com/techotaku/Wechat-SDK.php)[ Packagist](https://packagist.org/packages/techotaku/wechat-sdk)[ Docs](https://github.com/techotaku/Wechat-SDK.php)[ RSS](/packages/techotaku-wechat-sdk/feed)WikiDiscussions master Synced 3w ago

READMEChangelog (2)Dependencies (1)Versions (3)Used By (0)

微信公众平台SDK
=========

[](#微信公众平台sdk)

[![Latest Stable Version](https://camo.githubusercontent.com/826aa23961db5c383eef5554bcf3a564b586feb7c444c69c99a9c96ed42099bf/68747470733a2f2f706f7365722e707567782e6f72672f746563686f74616b752f7765636861742d73646b2f762f737461626c652e706e67)](https://packagist.org/packages/techotaku/wechat-sdk) [![Total Downloads](https://camo.githubusercontent.com/24ed01907f5db865a963be7ad13a3ff8c16ad1cf3ff87a78facfe019d3eaa588/68747470733a2f2f706f7365722e707567782e6f72672f746563686f74616b752f7765636861742d73646b2f646f776e6c6f6164732e706e67)](https://packagist.org/packages/techotaku/wechat-sdk) [![Build Status](https://camo.githubusercontent.com/b7babc709c7b8fa2f7b2630dc1f9d4d3b351dce8f2f738b4308bf6a4fd48e61e/68747470733a2f2f7472617669732d63692e6f72672f746563686f74616b752f5765636861742d53444b2e7068702e706e673f6272616e63683d6d6173746572)](https://travis-ci.org/techotaku/Wechat-SDK.php) [![Coverage Status](https://camo.githubusercontent.com/bee11d8c4344fb4e8eef11cbae0cbfaa66de945508238ee6906718436d63edfc/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f746563686f74616b752f5765636861742d53444b2e7068702f62616467652e706e673f6272616e63683d6d6173746572)](https://coveralls.io/r/techotaku/Wechat-SDK.php?branch=master)

Overview
--------

[](#overview)

PHP版本的微信公众平台SDK。可以很方便地解析请求、发送回复。

Installation
------------

[](#installation)

#### Composer

[](#composer)

把下面的配置代码加入你的`composer.json`的`require`段。

```
"techotaku/wechat-sdk": ">=1.0.1"
```

然后使用[Composer](https://getcomposer.org/)来安装SDK。

```
composer install
```

如果[Packagist](https://packagist.org)故障或者不可用导致无法安装SDK的，可以使用[Satis](https://github.com/composer/satis "Satis - Package Repository Generator")或者Artifact来进行本地安装，详见Composer文档中的[Repositories](https://getcomposer.org/doc/05-repositories.md#hosting-your-own)。

#### Manually

[](#manually)

复制src/Wechat.php到任意位置，然后`require`或者`require_once`。

Usage
-----

[](#usage)

#### Autoload

[](#autoload)

如果你使用了Composer来安装SDK，使用以下代码即可完成自动加载的配置。

```
require 'vendor/autoload.php';
```

SDK位于全局命名空间下。

#### Initialization

[](#initialization)

实例化`Wechat`即可完成初始化。

```
define('TOKEN', ''); // 微信通信令牌，在公众平台管理后台设置
define('DEBUG', TRUE); // 调试模式开关，指示是否将错误信息通过文本消息回复（如果可能）。
$wechat = new \Wechat(TOKEN, DEBUG);
```

初始化之后SDK将尝试从`$_GET[]`和`$GLOBALS['HTTP_RAW_POST_DATA']`中读取信息解析请求并进行初步处理。

#### Parsing and processing

[](#parsing-and-processing)

SDK提供以下方法对请求进行解析。

- `isApiValidation()`：返回一个bool值，指示当前请求是否为微信公众平台进行开发者验证的echoback请求。当结果为`TRUE`时，SDK已经将`echostr`的内容输出，SDK的调用方在此分支逻辑中请勿继续输出信息，否则会导致验证失败。（SDK并未使用常见的`exit()`来处理echoback，因此在判断为验证请求之后，PHP脚本执行并不会中止，调用方可以继续处理“验证请求”这一分支的剩余逻辑，只要不再进行输出即可。）当结果为`TRUE`时，`isValid()`的结果必定为`FALSE`。
- `isValid()`：返回一个bool值，指示当前请求是否为一个有效的请求。有效的请求是指，当前请求的签名正确、POST数据为可以被解析的XML、解析后的XML数据至少包括消息发送者和消息接收者。
- `getRequestType()`：返回一个字符串，指示请求类型。请求类型的定义如下（建议使用类常量，不要直接使用字符串）：

```
  /**
   * 微信公众平台传入消息类
   * 包含传入消息类型常量定义
   */
  class WechatRequest {
    const text = 'text';
    const image = 'image';
    const location = 'location';
    const link = 'link';
    const subscribe = 'subscribe';
    const voice = 'voice';
    const unsubscribe = 'unsubscribe';
    const unknown = 'unknown';
  }
```

- `getRequest([$key])`：根据给定的可选参数key返回请求中携带的数据，若对应的key不存在则返回`FALSE`。若省略参数key，则返回完整的请求信息数组。

SDK提供以下方法回复消息。

- `sendResponse($type, $params)`： 回复指定类型的消息。方法原型及参数说明如下：

```
    /**
     * 回复消息
     *
     * @param  string  $type     消息类型，在类WechatResponse中定义
     * @param  string  $params   消息参数，与消息类型相关：
     *     WechatResponse::text  文本消息  $params为消息内容
     *         $params                        消息文本
     *     WechatResponse::news  图文消息  $params为数组
     *         $params                        由单条图文消息类型 WechatNewsResponseItem 组成的数组
     *     WechatResponse::music 音乐消息  $params为关联数组
     *         $params['title']
     *         $params['description']
     *         $params['musicUrl']
     *         $params['hqMusicUrl']
     * @return void
     */
    public function sendResponse($type, $params)
```

单条图文消息类`WechatNewsResponseItem`的数组构造示例如下：

```
$array = array(
            new WechatNewsResponseItem('图文消息标题', '图文消息说明', '图片地址', '点击转向的链接'),
            new WechatNewsResponseItem('图文消息标题', '图文消息说明', '图片地址', '点击转向的链接')
            );
```

License
-------

[](#license)

The MIT License (MIT)
Copyright (c) 2013 Ian Li

See LICENSE

###  Health Score

28

—

LowBetter than 52% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity15

Limited adoption so far

Community5

Small or concentrated contributor base

Maturity59

Maturing project, gaining track record

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

2

Last Release

4682d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/2851f66c5f20e84dbe2cd642054b40afbf65921816bab58feda2ba059b304a19?d=identicon)[techotaku](/maintainers/techotaku)

---

Tags

sdkwechat

### Embed Badge

![Health badge](/badges/techotaku-wechat-sdk/health.svg)

```
[![Health](https://phpackages.com/badges/techotaku-wechat-sdk/health.svg)](https://phpackages.com/packages/techotaku-wechat-sdk)
```

###  Alternatives

[thenbsp/wechat

微信公众平台第三方 SDK 开发包，优雅、健壮，可扩展，遵循 PSR 开发规范。

9408.2k](/packages/thenbsp-wechat)[itxiao6/wechat

MinKernel.Wechat

1062.8k](/packages/itxiao6-wechat)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
