rjwu123 / wechat-oauth
WeChat OAuth
1.0.0
2018-09-20 09:26 UTC
Requires
- php: >= 5.4.0
This package is not auto-updated.
Last update: 2024-09-15 04:43:39 UTC
README
微信OAuth登录
Installation
Composer (推荐)
把下面的配置加入你的composer.json
文件
"rjwu123/wechat-oauth": "dev-master"
然后使用Composer来安装SDK
composer install
Usage
Autoload
如果你用Composer来安装,可以用以下代码自动加载
require 'vendor/autoload.php';
SDK位于全局命名空间下。
use rjwu123\wechat\Oauth
Initialization
实例化OAuth
即可完成初始化
$oauth = new \rjwu123\wechat\OAuth($options);
$appid
和$secret
是微信开放平台的应用的唯一标识和秘钥AppSecret
Code samples
require 'vendor/autoload.php'; use rjwu123\wechat\Oauth; $oauth = new Oauth([ 'appid' => 'xxxx', 'appsecret' => 'xxxx', 'redirectUri' => 'http://www.test.com', 'scope' => 'snsapi_userinfo' ]); // 第一步:用户同意授权,获取code $authUrl = $oauth->getAuthorizationUrl(); if (!isset($_GET['code'])) { $oauth->redirect($authUrl); } else { // 第二步:通过code换取网页授权access_token $res = $oauth->getAccessToken('authorization_code', [ 'code' => $_GET['code'] ]); if (isset($res['errcode']) && $res['errcode'] > 0) { $oauth->redirect($authUrl); return; } else { // 第四步:拉取用户信息(需scope为 snsapi_userinfo) if ($res['scope'] == 'snsapi_userinfo') { $info = $oauth->getUserInfo($res); } } // 第三步:刷新access_token $url = $oauth->getAccessToken('refresh_token', [ 'refresh_token' => $res['refresh_token'] ]); }