PHPackages                             qcloud/weapp-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. qcloud/weapp-sdk

ActiveLibrary

qcloud/weapp-sdk
================

php sdk for qcloud weapp server

v2.2.2(7y ago)24912.2k↓100%62[6 issues](https://github.com/tencentyun/wafer-php-server-sdk/issues)MITPHPPHP &gt;=5.4.0

Since Oct 18Pushed 7y ago40 watchersCompare

[ Source](https://github.com/tencentyun/wafer-php-server-sdk)[ Packagist](https://packagist.org/packages/qcloud/weapp-sdk)[ Docs](https://www.qcloud.com/)[ RSS](/packages/qcloud-weapp-sdk/feed)WikiDiscussions master Synced today

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

Wafer 服务端 SDK - PHP
===================

[](#wafer-服务端-sdk---php)

[![Latest Stable Version](https://camo.githubusercontent.com/e284330dedee9d942b01e75657917c10dcdbdbbd351ed34a6168ef96f11f99d4/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f71636c6f75642f77656170702d73646b2e737667)](https://packagist.org/packages/qcloud/weapp-sdk)[![License](https://camo.githubusercontent.com/453393cfa5c92da45ab09b7801b545eee8738e3c7de629b2955dc5a5c876ffe3/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f74656e63656e7479756e2f77616665722d7068702d7365727665722d73646b2e737667)](LICENSE)

#### 注意

[](#注意)

Wafer PHP SDK 已经全面升级 2.0 以支持 Wafer2，如果您想查看 Wafer1 的 PHP SDK 文档，请[点击这里](https://github.com/tencentyun/wafer-php-server-sdk/tree/v0.4.4)。

介绍
--

[](#介绍)

Wafer 服务端 SDK 是腾讯云为微信小程序开发者提供的快速开发库，SDK 封装了以下功能供小程序开发者快速调用：

- 用户登录与验证
- 信道服务
- 图片上传
- 数据库

开发者只需要根据文档对 SDK 进行初始化配置，就可以获得以上能力。你还可以直接到[腾讯云小程序控制台](https://console.qcloud.com/la)购买小程序解决方案，可以得到运行本示例所需的资源和服务，其中包括已部署好的相关程序、示例代码及自动下发的 SDK 配置文件 `/etc/qcloud/sdk.config`。

安装
--

[](#安装)

- 方法一（推荐）：使用 PHP 包依赖管理工具 `composer` 执行以下命令安装

```
composer require qcloud/weapp-sdk
```

- 方法二： 直接下载本仓库 `ZIP` 包解压到项目目录中

API
---

[](#api)

参见 [API 文档](./API.md)

使用
--

[](#使用)

### 加载 SDK

[](#加载-sdk)

```
// 方法一：使用 composer 加载
require_once 'path/to/vendor/autoload.php';

// 方法二：不使用 composer 加载
require_once 'path/to/qcloud/weapp-sdk/AutoLoader.php';
```

### 初始化 SDK 配置项

[](#初始化-sdk-配置项)

```
use \QCloud_WeApp_SDK\Conf as Config;

Config::setup(array(
    'appId'          => '微信小程序 AppID',
    'appSecret'      => '微信小程序 AppSecret',
    'useQcloudLogin' => false,
    'mysql' => [
        'host' => 'localhost',
        'port' => 3306,
        'user' => 'root',
        'pass' => '',
        'db'   => 'cAuth',
        'char' => 'utf8mb4'
    ],
    'cos' => [
        'region'       => 'cn-south',
        'fileBucket'   => 'test',
        'uploadFolder' => ''
    ],
    'serverHost'         => '1234567.qcloud.la',
    'tunnelServerUrl'    => '1234567.ws.qcloud.la',
    'tunnelSignatureKey' => 'abcdefghijkl',
    'qcloudAppId'        => '121000000',
    'qcloudSecretId'     => 'ABCDEFG',
    'qcloudSecretKey'    => 'abcdefghijkl',
    'wxMessageToken'     => 'abcdefghijkl',
));
```

具体配置项说明请查看：[API 文档](/API.md#sdk-%E9%85%8D%E7%BD%AE)。

### 处理用户登录请求

[](#处理用户登录请求)

```
use \QCloud_WeApp_SDK\Auth\LoginService;
use \QCloud_WeApp_SDK\Constants;

$result = LoginService::login();

// $result => [
//   loginState: 1  // 1表示登录成功，0表示登录失败
//   userinfo: []   // 用户信息
// ]

if ($result['loginState'] === Constants::S_AUTH) {
    // 微信用户信息：`$result['userinfo']['userinfo']`
} else {
    // 登录失败原因：`$result['error']`
}
```

### 检查请求登录态

[](#检查请求登录态)

```
use \QCloud_WeApp_SDK\Auth\LoginService;
use \QCloud_WeApp_SDK\Constants;

$result = LoginService::check();

// $result => [
//   loginState: 1  // 1表示登录成功，0表示登录失败
//   userinfo: []   // 用户信息
// ]

if ($result['loginState'] === Constants::E_AUTH) {
    // 登录失败原因：`$result['error']`
    return;
}

// 使用微信用户信息（`$result['userinfo']['userinfo']`）处理其它业务逻辑
// ...
```

### 使用信道服务

[](#使用信道服务)

业务在一个路由上（如 `/tunnel`）提供信道服务，只需把该路由上的请求都交给 SDK 的信道服务处理即可。

```
use \QCloud_WeApp_SDK\Tunnel\TunnelService;
use \QCloud_WeApp_SDK\Tunnel\ITunnelHandler;

class TunnelHandler implements ITunnelHandler {
    // TODO: 传入登录的用户信息
    public function __construct($userinfo) {

    }

    // TODO: 实现 onRequest 方法，处理信道连接请求
    public function onRequest($tunnelId, $tunnelUrl) {

    }

    // TODO: 实现 onConnect 方法，处理信道连接事件
    public function onConnect($tunnelId) {

    }

    // TODO: 实现 onMessage 方法，处理信道消息
    public function onMessage($tunnelId, $type, $content) {

    }

    // TODO: 实现 onClose 方法，处理信道关闭事件
    public function onClose($tunnelId) {

    }
}

$handler = new TunnelHandler();
TunnelService::handle($handler, array('checkLogin' => TRUE));
```

使用信道服务需要实现处理器，来获取处理信道的各种事件，具体可参考接口 [ITunnelHandler](/API.md#itunnelhandler) 的 API 文档以及配套 Demo 中的 [ChatTunnelHandler](/application/business/ChatTunnelHandler.php) 的实现。

### MySQL 操作类

[](#mysql-操作类)

SDK 在 PDO 的基础上完成了对增删改查等常用操作的封装，并默认会在初始化 SDK 的时候连接数据库，直接通过如下代码可以快速使用 MySQL 操作类：

> \*\*注意：\*\*MySQL 操作类为静态类

```
use \QCloud_WeApp_SDK\Mysql\Mysql as DB;

// 查询数据
$res = DB::row('cSessionInfo', ['*'], ['open_id' => '1234567890']);     // 查询一条
$res = DB::select('cSessionInfo', ['*'], ['open_id' => '1234567890']);  // 查询多条

// 插入数据
$res = DB::insert('cSessionInfo', ['open_id' => '1234567890']);

// 更新数据
$res = DB::update('cSessionInfo', ['open_id' => '1234567890'], ['uuid' => '1']);

// 删除数据
$res = DB::delete('cSessionInfo', ['open_id' => '1234567890']);
```

具体配置项说明请查看：[API 文档](/API.md#MySQL)。

### COS 实例

[](#cos-实例)

SDK 导出了一个 COS V5 API 实例，可以使用以下代码获取：

```
use \QCloud_WeApp_SDK\Cos\CosAPI as Cos;

$cosClient = Cos::getInstance();
$cosClient->upload('mybucket', 'test.txt', 'Hello World')->toArray();
```

更多关于 `Cos::getInstance()` 返回 COS 实例的 API，可以查看 [COS PHP SDK V5 文档](https://github.com/tencentyun/cos-php-sdk-v5)

### 详细示例

[](#详细示例)

参见项目：[Wafer2 服务端 DEMO - PHP](https://github.com/tencentyun/wafer2-quickstart-php)

LICENSE
-------

[](#license)

[MIT](LICENSE)

###  Health Score

43

—

FairBetter than 91% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity44

Moderate usage in the ecosystem

Community28

Small or concentrated contributor base

Maturity67

Established project with proven stability

 Bus Factor1

Top contributor holds 89.1% 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.

###  Release Activity

Cadence

Every ~36 days

Recently: every ~61 days

Total

18

Last Release

2824d ago

Major Versions

v0.4.4 → v2.02017-10-16

### Community

Maintainers

![](https://www.gravatar.com/avatar/1e6735c4ecc810aa61a07b3507e71c2756fb9e72ba8ba83347c86af34f1e9c67?d=identicon)[lewzylu](/maintainers/lewzylu)

![](https://www.gravatar.com/avatar/c3cca15dbd87d077996083f4ee810a8ab0cdc188403a5492aa151fa1abf169ff?d=identicon)[xujianguo](/maintainers/xujianguo)

![](https://www.gravatar.com/avatar/5414c239547953601faec40fd490e42400e90611f51dfc1b4313540a440b5264?d=identicon)[jas0ncn](/maintainers/jas0ncn)

![](https://www.gravatar.com/avatar/5b3d1c3b0e82d5ce34aa4eb04d6976d721aeefb0d0f464f5f5fe5ad964dc656d?d=identicon)[miusuncle](/maintainers/miusuncle)

![](https://www.gravatar.com/avatar/ca88b15ebd4f9a458034bbc035f00eee96cf54cb530509d77ecafe3c67e10c3e?d=identicon)[yaozongyou](/maintainers/yaozongyou)

---

Top Contributors

[![miusuncle](https://avatars.githubusercontent.com/u/7549857?v=4)](https://github.com/miusuncle "miusuncle (131 commits)")[![jas0ncn](https://avatars.githubusercontent.com/u/3380894?v=4)](https://github.com/jas0ncn "jas0ncn (9 commits)")[![techird](https://avatars.githubusercontent.com/u/1901286?v=4)](https://github.com/techird "techird (5 commits)")[![khs1994](https://avatars.githubusercontent.com/u/16733187?v=4)](https://github.com/khs1994 "khs1994 (1 commits)")[![xiaoshangmin](https://avatars.githubusercontent.com/u/6623100?v=4)](https://github.com/xiaoshangmin "xiaoshangmin (1 commits)")

---

Tags

sdkqcloudweapp

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/qcloud-weapp-sdk/health.svg)

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

###  Alternatives

[qcloudsms/qcloudsms_php

qcloud sms php sdk

116261.0k17](/packages/qcloudsms-qcloudsms-php)[freyo/flysystem-qcloud-cos-v5

Flysystem Adapter for Tencent Qcloud COS SDK V5

156194.1k6](/packages/freyo-flysystem-qcloud-cos-v5)[tencentyun-api/qcloudapi-sdk-php

php sdk for qcloudapi

10948.7k](/packages/tencentyun-api-qcloudapi-sdk-php)[tencentyun/php-sdk

php sdk for app.qcloud.com image service

271.6k](/packages/tencentyun-php-sdk)[tencentyun/cos-php-sdk

php sdk for www.qcloud.com cos service

201.4k1](/packages/tencentyun-cos-php-sdk)

PHPackages © 2026

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