PHP code example of tourze / baidu-oauth2-integrate-bundle
1. Go to this page and download the library: Download tourze/baidu-oauth2-integrate-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 / baidu-oauth2-integrate-bundle example snippets
use Tourze\BaiduOauth2IntegrateBundle\Service\BaiduOAuth2Service;
class AuthController extends AbstractController
{
public function __construct(
private BaiduOAuth2Service $oauth2Service
) {}
#[Route('/baidu/login', name: 'baidu_login')]
public function login(): Response
{
$authUrl = $this->oauth2Service->generateAuthorizationUrl();
return $this->redirect($authUrl);
}
}
class BaiduOAuth2Service
{
// Generate authorization URL
public function generateAuthorizationUrl(?string $sessionId = null): string
// Handle authorization callback
public function handleCallback(string $code, string $state): BaiduOAuth2User
// Refresh access token
public function refreshToken(string $refreshToken): array
}
class BaiduApiClient
{
// Get user information
public function getUserInfo(string $accessToken): array
// Refresh token
public function refreshToken(string $refreshToken, string $clientId, string $clientSecret): array
}
class BaiduOAuth2Config
{
private ?int $id; // Configuration ID
private string $clientId; // Baidu API Key
private string $clientSecret; // Baidu Secret Key
private ?string $scope; // Authorization scope
private bool $valid; // Is enabled
private \DateTime $createdAt; // Created time
private \DateTime $updatedAt; // Updated time
}
class BaiduOAuth2User
{
private ?int $id; // User ID
private string $openid; // Baidu OpenID
private ?string $unionid; // Baidu UnionID
private ?string $accessToken; // Access token
private ?string $refreshToken; // Refresh token
private ?\DateTime $tokenExpiresAt; // Token expiration time
private ?array $userInfo; // User information
private \DateTime $createdAt; // Created time
private \DateTime $updatedAt; // Updated time
}
class BaiduOAuth2State
{
private ?int $id; // State ID
private string $state; // State token
private ?string $sessionId; // Session ID
private bool $used; // Is used
private \DateTime $expiresAt; // Expiration time
private BaiduOAuth2Config $config; // Associated configuration
private \DateTime $createdAt; // Created time
private \DateTime $updatedAt; // Updated time
}