PHPackages                             luckyhjh/wechat-mini-program - 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. luckyhjh/wechat-mini-program

ActiveLibrary[API Development](/categories/api)

luckyhjh/wechat-mini-program
============================

The Wechat Mini Program SDK

v1.1.0(1y ago)31142MITPHPPHP &gt;=5.4

Since Feb 26Pushed 1y ago2 watchersCompare

[ Source](https://github.com/LuckyHJH/WechatMiniProgram)[ Packagist](https://packagist.org/packages/luckyhjh/wechat-mini-program)[ RSS](/packages/luckyhjh-wechat-mini-program/feed)WikiDiscussions master Synced today

READMEChangelog (3)DependenciesVersions (5)Used By (0)

微信小程序 SDK
=========

[](#微信小程序-sdk)

用于 PHP 方便调用微信小程序的 [服务端接口](https://developers.weixin.qq.com/miniprogram/dev/api-backend/)

[![Latest Stable Version](https://camo.githubusercontent.com/568167cdd7e42d91316d1c3521c5ef13cacb032a6ee5eff5b789787f3e1e902e/68747470733a2f2f706f7365722e707567782e6f72672f6c75636b79686a682f7765636861742d6d696e692d70726f6772616d2f76)](//packagist.org/packages/luckyhjh/wechat-mini-program)[![Total Downloads](https://camo.githubusercontent.com/258295f12595e27d704c322adad74e7d8a117e1a5dae5f2204092171cabf0957/68747470733a2f2f706f7365722e707567782e6f72672f6c75636b79686a682f7765636861742d6d696e692d70726f6772616d2f646f776e6c6f616473)](//packagist.org/packages/luckyhjh/wechat-mini-program)[![Latest Unstable Version](https://camo.githubusercontent.com/896e789e4cd620c3119322b4bd97e85929a22aca1fb3e7d701dc4fa689a5fba7/68747470733a2f2f706f7365722e707567782e6f72672f6c75636b79686a682f7765636861742d6d696e692d70726f6772616d2f762f756e737461626c65)](//packagist.org/packages/luckyhjh/wechat-mini-program)[![License](https://camo.githubusercontent.com/72332d71d6411f311652ec7562a88cf1f9ac2555d027a838f3e49a749f349421/68747470733a2f2f706f7365722e707567782e6f72672f6c75636b79686a682f7765636861742d6d696e692d70726f6772616d2f6c6963656e7365)](//packagist.org/packages/luckyhjh/wechat-mini-program)

接入要求
----

[](#接入要求)

"php": "&gt;=5.4"

接入方式
----

[](#接入方式)

### 1、composer 安装

[](#1composer-安装)

composer require luckyhjh/wechat-mini-program

### 2、新建文件

[](#2新建文件)

新建类继承 WechatMiniProgram，并实现相关接口，token 可保存在 redis 或 mysql 等。

例如 Laravel/Lumen，可新建文件 app/Libs/WechatMiniProgram.php：

```
namespace App\Libs;

use Illuminate\Support\Facades\Cache;
use WechatMiniProgram\Api\AccessToken;
use WechatMiniProgram\ApiException;

class WechatMiniProgram extends \WechatMiniProgram\WechatMiniProgram
{
    public function __construct($appid = '', $secret = '')
    {
        empty($appid) and $appid = env('WX_APPID');
        empty($secret) and $secret = env('WX_SECRET');
        parent::__construct($appid, $secret);
    }

    /**
     * 读取token
     * @return string
     */
    public function getAccessToken()
    {
        $cache_name = 'wx_token_' . $this->getAppid();
        $access_token = Cache::get($cache_name);
        if ($access_token) {
            return $access_token;
        }

        $AccessToken = new AccessToken($this);
        try {
            $Token = $AccessToken->getAccessToken();
            $access_token = $Token->access_token;
            $this->setAccessToken($access_token, $Token->expires_in);
            return $access_token;

        } catch (ApiException $apiException) {
            return '';
        }
    }

    /**
     * 保存token
     * @param string $access_token
     * @param int $expire
     * @return bool
     */
    public function setAccessToken($access_token, $expire = 7200)
    {
        $cache_name = 'wx_token_' . $this->getAppid();
        return Cache::set($cache_name, $access_token, $expire);
    }
}
```

例如 ThinkPHP，可新建文件 extend/WechatMiniProgram.php：

```
use WechatMiniProgram\Api\AccessToken;
use WechatMiniProgram\ApiException;

class WechatMiniProgram extends \WechatMiniProgram\WechatMiniProgram
{
    public function __construct($appid = '', $secret = '')
    {
        empty($appid) and $appid = env('WX_APPID');
        empty($secret) and $secret = env('WX_SECRET');
        parent::__construct($appid, $secret);
    }

    /**
     * 读取token
     * @return string
     */
    public function getAccessToken()
    {
        $cache_name = 'wx_token_' . $this->getAppid();
        $access_token = cache($cache_name);
        if ($access_token) {
            return $access_token;
        }

        $AccessToken = new AccessToken($this);
        try {
            $Token = $AccessToken->getAccessToken();
            $access_token = $Token->access_token;
            $this->setAccessToken($access_token, $Token->expires_in);
            return $access_token;

        } catch (ApiException $apiException) {
            return '';
        }
    }

    /**
     * 保存token
     * @param string $access_token
     * @param int $expire
     * @return bool
     */
    public function setAccessToken($access_token, $expire = 7200)
    {
        $cache_name = 'wx_token_' . $this->getAppid();
        return cache($cache_name, $access_token, $expire);
    }
}
```

### 3、业务调用

[](#3业务调用)

业务里根据需要调用

```
$mp = new WechatMiniProgram();
$Auth = new Auth($mp);
$User = new \WechatMiniProgram\Api\User($mp);//如果要调用其它接口，可复用$mp
try {
    $session = $Auth->code2session($code);
    $openid = $session->openid;
} catch (ApiException $apiException) {
}
```

支持接口
----

[](#支持接口)

- AccessToken-&gt;getAccessToken()：[获取小程序全局唯一后台接口调用凭据](https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/access-token/auth.getAccessToken.html)
- Auth-&gt;code2session()：[用 code 获取 openid](https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/login/auth.code2Session.html)
- User-&gt;getPhoneNumber()：[获取手机号](https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/getPhoneNumber.html)
- SubscribeMessage-&gt;send()：[发送订阅消息](https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/subscribe-message/subscribeMessage.send.html)
- WxaCode-&gt;getUnlimited()：[生成小程序码，可接受页面参数较短，生成个数不受限](https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/qr-code/wxacode.getUnlimited.html)
- WxaCode-&gt;get()：[生成小程序码，可接受 path 参数较长，生成个数受限](https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/qr-code/wxacode.get.html)
- SecCheck-&gt;messageIsRisky()：[文本是否为风险内容](https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/sec-check/security.msgSecCheck.html)
- SecCheck-&gt;imageIsRisky()：[文本是否为风险内容](https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/sec-check/security.imgSecCheck.html)

License
-------

[](#license)

MIT

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance38

Infrequent updates — may be unmaintained

Popularity15

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity51

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% 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 ~454 days

Total

4

Last Release

536d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/2641108d52b4014d80592ab3cc4de4f1fc0a460c9538316c6091d37aad8501e7?d=identicon)[LuckyHJH](/maintainers/LuckyHJH)

---

Top Contributors

[![LuckyHJH](https://avatars.githubusercontent.com/u/3377393?v=4)](https://github.com/LuckyHJH "LuckyHJH (7 commits)")

---

Tags

sdkwechatweixin

### Embed Badge

![Health badge](/badges/luckyhjh-wechat-mini-program/health.svg)

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

###  Alternatives

[overtrue/laravel-wechat

微信 SDK for Laravel

3.0k1.8M53](/packages/overtrue-laravel-wechat)[thenbsp/wechat

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

9408.2k](/packages/thenbsp-wechat)[naixiaoxin/think-wechat

EasyWechat For Thnkphp5.1+

27316.1k1](/packages/naixiaoxin-think-wechat)

PHPackages © 2026

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