PHP code example of tourze / wechat-official-account-oauth2-bundle

1. Go to this page and download the library: Download tourze/wechat-official-account-oauth2-bundle 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/ */

    

tourze / wechat-official-account-oauth2-bundle example snippets


// config/bundles.php
return [
    // ...
    Tourze\WechatOfficialAccountOAuth2Bundle\WechatOfficialAccountOAuth2Bundle::class => ['all' => true],
];

use Tourze\WechatOfficialAccountOAuth2Bundle\Service\OAuth2AuthorizationService;

// Build WeChat authorization URL
$authUrl = $authorizationService->buildWechatAuthUrl($account, 'snsapi_userinfo', $redirectUri);

// Get user info by authorization code
$userInfo = $authorizationService->getUserInfoByCode($account, $code);

// Create internal access token
$accessToken = $authorizationService->exchangeCodeForToken($code, $redirectUri, $account);

use Tourze\WechatOfficialAccountOAuth2Bundle\Service\OAuth2UserInfoService;

// Get user info (via internal access token)
$userInfo = $userInfoService->getUserInfo($oAuth2AccessToken);

use Tourze\WechatOfficialAccountOAuth2Bundle\Service\WechatOAuth2Service;

// Build WeChat authorization URL
$authUrl = $wechatOAuth2Service->buildAuthorizationUrl($account, $redirectUri, 'snsapi_base');

// Get WeChat access token
$tokenData = $wechatOAuth2Service->getAccessTokenByCode($account, $code);

// Validate WeChat access token
$isValid = $wechatOAuth2Service->validateAccessToken($account, $accessToken, $openid);
bash
# Generate migration files
php bin/console doctrine:migrations:diff

# Execute migrations
php bin/console doctrine:migrations:migrate
text
GET /oauth2/authorize?client_id=CLIENT_ID&redirect_uri=REDIRECT_URI&scope=snsapi_base&state=STATE&response_type=code
bash
POST /oauth2/token
Content-Type: application/x-www-form-urlencoded

grant_type=authorization_code&code=AUTHORIZATION_CODE&redirect_uri=REDIRECT_URI&client_id=CLIENT_ID&client_secret=CLIENT_SECRET
bash
POST /oauth2/refresh
Content-Type: application/x-www-form-urlencoded

grant_type=refresh_token&refresh_token=REFRESH_TOKEN&client_id=CLIENT_ID&client_secret=CLIENT_SECRET
bash
POST /oauth2/revoke
Content-Type: application/x-www-form-urlencoded

token=ACCESS_TOKEN&client_id=CLIENT_ID&client_secret=CLIENT_SECRET
bash
# Configure OAuth2 settings
php bin/console wechat:oauth2:configure <account-id> [--scope=SCOPE] [--remark=REMARK]
bash
# PHPStan static analysis
vendor/bin/phpstan analyse packages/wechat-official-account-oauth2-bundle/src/

# PHP CS Fixer code formatting
vendor/bin/php-cs-fixer fix packages/wechat-official-account-oauth2-bundle/src/