这个组件基于 PHP League's OAuth 2.0 Client.
用composer安装:
composer require xiabin/oauth2-sinaweibo
$provider = new Xiabin\OAuth2\Client\Provider\SinaWeibo([
'clientId' => '{sinaweibo-client-id}',
'clientSecret' => '{sinaweibo-client-secret}',
'redirectUri' => 'https://example.com/callback-url',
]);
if (!isset($_GET['code'])) {
//如果没有code就去获取
$authUrl = $provider->getAuthorizationUrl();
$_SESSION['oauth2state'] = $provider->getState();
header('Location: '.$authUrl);
exit;
//判断state与之前使用的是否一致
} elseif (empty($_GET['state']) || ($_GET['state'] !== $_SESSION['oauth2state'])) {
unset($_SESSION['oauth2state']);
exit('Invalid state');
} else {
//获取accesstoken
$token = $provider->getAccessToken('authorization_code', [
'code' => $_GET['code']
]);
...
}
$ ./vendor/bin/phpunit
The MIT License (MIT). Please see License File for more information.