PHP code example of rjwu123 / wechat-oauth

1. Go to this page and download the library: Download rjwu123/wechat-oauth library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

rjwu123 / wechat-oauth example snippets




use rjwu123\wechat\Oauth

$oauth = new \rjwu123\wechat\OAuth($options);



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']
    ]);
}