PHPackages                             henter/wechat-oauth - 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. [Authentication &amp; Authorization](/categories/authentication)
4. /
5. henter/wechat-oauth

ActiveLibrary[Authentication &amp; Authorization](/categories/authentication)

henter/wechat-oauth
===================

WeChat OAuth SDK

1.0.1(10y ago)3443.2k↓39.1%10[1 PRs](https://github.com/henter/WeChat-OAuth/pulls)2MITPHPPHP &gt;=5.3.9

Since Sep 2Pushed 5y ago5 watchersCompare

[ Source](https://github.com/henter/WeChat-OAuth)[ Packagist](https://packagist.org/packages/henter/wechat-oauth)[ RSS](/packages/henter-wechat-oauth/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (1)Versions (3)Used By (2)

微信登录SDK
=======

[](#微信登录sdk)

[![Latest Stable Version](https://camo.githubusercontent.com/0218eab6bfc174666cc67c0172d488355525e06a08894a92b454fb0f7a0d156f/68747470733a2f2f706f7365722e707567782e6f72672f68656e7465722f7765636861742d6f617574682f762f737461626c652e706e67)](https://packagist.org/packages/henter/wechat-oauth) [![Total Downloads](https://camo.githubusercontent.com/1c7a108203824a9012b4bb8a071aeabdc913c5e2618312f80b52325f5145e833/68747470733a2f2f706f7365722e707567782e6f72672f68656e7465722f7765636861742d6f617574682f646f776e6c6f6164732e706e67)](https://packagist.org/packages/henter/wechat-oauth) [![Build Status](https://camo.githubusercontent.com/e8ff126e09be016285d3cf02c57be4b6c3daa872493fa33dd4940b1f67ab1f3a/68747470733a2f2f7472617669732d63692e6f72672f68656e7465722f5765436861742d4f417574682e706e673f6272616e63683d6d6173746572)](https://travis-ci.org/henter/WeChat-OAuth) [![Coverage Status](https://camo.githubusercontent.com/8d81db77a265ed19f0613d3dcc6140ddb18379f407480abc8a1a6ffae311ba5c/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f68656e7465722f5765436861742d4f417574682f62616467652e706e673f6272616e63683d6d6173746572)](https://coveralls.io/r/henter/WeChat-OAuth)

Overview
--------

[](#overview)

微信OAuth登录SDK

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

[](#installation)

#### Composer (推荐)

[](#composer-推荐)

把下面的配置加入你的`composer.json`文件

```
"henter/wechat-oauth": "dev-master"
```

然后使用[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)

复制`lib/Henter/WeChat`到项目目录，然后`require "/path/to/sdk/OAuth.php"`

Usage
-----

[](#usage)

#### Autoload

[](#autoload)

如果你用Composer来安装，可以用以下代码自动加载

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

SDK位于全局命名空间下。

```
use Henter\WeChat\OAuth
```

#### Initialization

[](#initialization)

实例化`OAuth`即可完成初始化

```
$oauth = new \Henter\WeChat\OAuth($appid, $secret);
```

`$appid`和`$secret`是微信开放平台的应用的唯一标识和秘钥AppSecret

#### Code samples

[](#code-samples)

##### 登录

[](#登录)

```
$oauth = new \Henter\WeChat\OAuth($appid, $secret);
$callback_url = 'http://your_site.com/your_callback_url';
$url = $oauth->getAuthorizeURL($callback_url);
```

重定向到`$url`，待用户允许授权后，将会重定向到`$callback_url`上，并且带上`code`和`state`参数（示例代码未传入`state`参数）

默认授权地址是跳转到微信扫描二维码页面（适用于PC端），如果用户在微信内访问网页点微信登陆，这种方式不太适合。 需要用下面的方法获取用于微信内的授权地址：

```
$url = $oauth->getWeChatAuthorizeURL($callback_url);
```

注：这个在微信开放平台文档上没有（只在公众号平台文档有提到），不过测试发现同样适用于开放平台应用。

##### 通过`code`参数获取`access_token`

[](#通过code参数获取access_token)

```
//获取code参数
$code = $_GET['code'];

$oauth = new \Henter\WeChat\OAuth($appid, $secret);
if($access_token = $oauth->getAccessToken('code', $code)){
	$refresh_token = $oauth->getRefreshToken();
	$expires_in = $oauth->getExpiresIn();
	$openid = $oauth->getOpenid();
}else{
	echo $oauth->error();
}
```

如果获取成功，需保存这4个值用于后续接口调用，否则通过`$oauth->error()`获取错误信息

###### 通过`access_token`调用API

[](#通过access_token调用api)

```
$oauth = new \Henter\WeChat\OAuth($appid, $secret, $access_token);
```

或

```
$oauth = new \Henter\WeChat\OAuth($appid, $secret);
$oauth->setAccessToken($access_token);
```

调用用户信息，需传入`openid`

```
$userinfo = $oauth->api('sns/userinfo', array('openid'=>$openid));
```

其中`sns/userinfo`为api类型，具体请参考[微信API文档](https://open.weixin.qq.com/cgi-bin/frame?t=resource/res_main_tmpl&verify=1&lang=zh_CN&target=res/web_wx_powered_interface)

##### 通过`refresh_token`刷新或续期`access_token`

[](#通过refresh_token刷新或续期access_token)

```
$oauth = new \Henter\WeChat\OAuth($appid, $secret);

//以下两种方式一样
$access_token = $oauth->getAccessToken('token', $refresh_token);
或
$access_token = $oauth->refreshAccessToken($refresh_token);
```

此时可以通过`$oauth->getRefreshToken()`获取新的`refresh_token`

#### 其它

[](#其它)

本SDK无任何抛异常部分，调用`$oauth->getAccessToken()`或`$oauth->api()`等方法时如果返回`false`则表示未成功，错误信息均通过`$oauth->error()`获取，所以无需使用`try {} catch {}`方式处理错误

License
-------

[](#license)

The MIT License (MIT) Copyright (c) 2014 Henter &lt;&gt;

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

###  Health Score

38

—

LowBetter than 85% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity41

Moderate usage in the ecosystem

Community17

Small or concentrated contributor base

Maturity59

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 ~433 days

Total

2

Last Release

3844d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/239970?v=4)[Henter](/maintainers/henter)[@henter](https://github.com/henter)

---

Top Contributors

[![henter](https://avatars.githubusercontent.com/u/239970?v=4)](https://github.com/henter "henter (23 commits)")

---

Tags

oauthphpwechatwechat-oauthoauthloginwechatweixinweixinoauthwechatoauthweixinloginwechatloginwechat-oauthweixin-oauth

### Embed Badge

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

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

###  Alternatives

[overtrue/socialite

A collection of OAuth 2 packages.

1.4k5.5M87](/packages/overtrue-socialite)[auth0/auth0-php

PHP SDK for Auth0 Authentication and Management APIs.

40820.2M68](/packages/auth0-auth0-php)[auth0/login

Auth0 Laravel SDK. Straight-forward and tested methods for implementing authentication, and accessing Auth0's Management API endpoints.

2745.0M3](/packages/auth0-login)[oakhope/oauth2-wechat

微信登录认证授权 Wechat login authorization. This package provides Wechat OAuth 2.0 support for the PHP League's OAuth 2.0 Client

228.9k](/packages/oakhope-oauth2-wechat)

PHPackages © 2026

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