PHPackages                             iamfat/wechat - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. iamfat/wechat

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

iamfat/wechat
=============

Composer Support for Netputer Wechat PHP SDK

03.0k↓66.7%PHP

Since Sep 22Pushed 9y ago1 watchersCompare

[ Source](https://github.com/iamfat/wechat-php-sdk)[ Packagist](https://packagist.org/packages/iamfat/wechat)[ RSS](/packages/iamfat-wechat/feed)WikiDiscussions master Synced 3d ago

READMEChangelogDependenciesVersions (1)Used By (0)

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

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

介绍
--

[](#介绍)

简单的微信公众平台 PHP SDK ，通过调用相应的接口，使你可以轻松地开发微信 App 。测试方法如下：

1. Clone 或下载项目源码，上传至服务器。
2. 进入[微信公众平台](https://mp.weixin.qq.com/)，高级功能，开启开发模式，并设置接口配置信息。修改 `URL` 为 `/example/server.php` 的实际位置，修改 `Token` 为 `weixin` （可自行在 `/example/server.php` 中更改）。
3. 向你的微信公众号发送消息并测试吧！

安装方法
----

[](#安装方法)

### composer

[](#composer)

```
composer require iamfat/wechat dev-master
```

用法
--

[](#用法)

### 服务端

[](#服务端)

直接浏览 `/example/server.php` 了解基本用法，以下为详细说明。

通过继承 `Wechat` 类进行扩展，通过重写 `onSubscribe()` 等方法响应关注等请求：

```
class MyWechat extends \Wechat\Server {
  protected function onSubscribe() {} // 用户关注
  protected function onUnsubscribe() {} // 用户取消关注

  protected function onText() {
    // 收到文本消息时触发，此处为响应代码
  }

  protected function onImage() {} // 收到图片消息
  protected function onLocation() {} // 收到地理位置消息
  protected function onLink() {} // 收到链接消息
  protected function onUnknown() {} // 收到未知类型消息
}
```

---

使用 `getRequest()` 可以获取本次请求中的参数（不区分大小写）：

```
$this->getRequest();
// 无参数时，返回包含所有参数的数组

$this->getRequest('msgtype');
// 有参数且参数存在时，返回该参数的值

$this->getRequest('ghost');
// 有参数但参数不存在时，返回 NULL
```

所有请求均包含：

```
ToUserName    接收方帐号（该公众号ID）
FromUserName  发送方帐号（代表用户的唯一标识）
CreateTime    消息创建时间（时间戳）
MsgId         消息ID（64位整型）

```

文本消息请求：

```
MsgType  text
Content  文本消息内容

```

图片消息请求：

```
MsgType  image
PicUrl   图片链接

```

地理位置消息请求：

```
MsgType     location
Location_X  地理位置纬度
Location_Y  地理位置经度
Scale       地图缩放大小
Label       地理位置信息

```

链接消息请求：

```
MsgType      link
Title        消息标题
Description  消息描述
Url          消息链接

```

事件推送：

```
MsgType   event
Event     事件类型
EventKey  事件 Key 值，与自定义菜单接口中 Key 值对应

```

其中，事件类型 `Event` 的值包括以下几种：

```
subscribe    关注
unsubscribe  取消关注
CLICK        自定义菜单点击事件（未验证）

```

---

使用 `responseText()` 方法回复文本消息：

```
$this->responseText(
  $content,  // 消息内容
  $funcFlag  // 可选参数（默认为0），设为1时星标刚才收到的消息
);
```

使用 `responseMusic()` 方法回复音乐消息：

```
$this->responseMusic(
  $title,        // 音乐标题
  $description,  // 音乐描述
  $musicUrl,     // 音乐链接
  $hqMusicUrl,   // 高质量音乐链接，Wi-Fi 环境下优先使用
  $funcFlag      // 可选参数，默认为0，设为1时星标刚才收到的消息
);
```

使用 `responseNews()` 方法回复图文消息：

```
$this->responseNews(
  $items,    // 由单条图文消息类型 NewsResponseItem() 组成的数组
  $funcFlag  // 可选参数，默认为0，设为1时星标刚才收到的消息
)
```

其中单条图文消息类型 `NewsResponseItem()` 格式如下：

```
$items[] = new NewsResponseItem(
  $title,        // 图文消息标题
  $description,  // 图文消息描述
  $picUrl,       // 图片链接
  $url           // 点击图文消息跳转链接
);
```

---

最后，实例化 `MyWechat()` 并调用 `handleRequest()` 方法即可运行。

```
// $token是你在公众平台设置的 Token
$wechat = new MyWechat($token);

$response = $wechat->handleRequest();
// $response是个返回的对象
// 通过(string)$response强制类型转换能获得返回给公众平台的XML字符串
echo $response;
```

如果你是使用框架, 希望另行导入`$_GET`和原始POST数据`$GLOBALS['HTTP_RAW_POST_DATA']`, handleRequest可以传入参数

```
$response = $wechat->handleRequest(['get'=>$your_get_data, 'post'=>$your_post_data])
```

### 单点登录

[](#单点登录)

```
$oauth = new \Wechat\OAuth($appId, $appSecret);
$openId = $oauth->getOpenId();  // OpenID会存储在`$_SESSION`中, 如果没有的话会自动触发HTTP跳转
```

### 应用接口

[](#应用接口)

```
$app = new \Wechat\App($appId, $appSecret);
// 获取`acess_token`
$token = $app->getAccessToken();
// 获取用户信息
$info = $app->getUserInfo($openId);
// 获取二维码数据
$qrdata = $app->getQRCode($sceneId, $permanent); // $permanent 是否是永久场景, 默认为false

// 获取JS相关信息
$js = new \Wechat\JS($app);
$url = 'http://path/to/api';
$package = $js->getSignPackage($url);   // 获取签名包
// appId, nonceStr, timestamp, url, signature, rawString

// 模板消息发送
$app->sendTemplateMessage($openId, $templateId, $data);
// $data = ['url'=>'xxxx', 'topcolor'=>, 其他的data]
```

TODO
----

[](#todo)

1. 完善文档和注释；
2. 完善异常处理；

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity20

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 54.8% of commits — single point of failure

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.

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/724105?v=4)[Jia Huang](/maintainers/iamfat)[@iamfat](https://github.com/iamfat)

---

Top Contributors

[![iamfat](https://avatars.githubusercontent.com/u/724105?v=4)](https://github.com/iamfat "iamfat (17 commits)")[![netputer](https://avatars.githubusercontent.com/u/549979?v=4)](https://github.com/netputer "netputer (11 commits)")[![undeadking](https://avatars.githubusercontent.com/u/914163?v=4)](https://github.com/undeadking "undeadking (2 commits)")[![kiddlee](https://avatars.githubusercontent.com/u/2315010?v=4)](https://github.com/kiddlee "kiddlee (1 commits)")

### Embed Badge

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

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

###  Alternatives

[yiister/yii2-advanced-grid

Advanced GridView extension for Yii framework 2

4712.5k](/packages/yiister-yii2-advanced-grid)[krifollk/module-code-generator

Code generator for magento 2

256.4k](/packages/krifollk-module-code-generator)

PHPackages © 2026

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