xiabin/oauth2-sinaweibo

Sina Weibo OAuth 2.0 Client Provider for The PHP League OAuth2-Client

v1.0.1 2016-07-04 08:27 UTC

This package is not auto-updated.

Last update: 2024-04-27 16:58:23 UTC


README

license Packagist Build Status Coverage Status Quality Score Packagist

这个组件基于 PHP League's OAuth 2.0 Client.

安装

用composer安装:

composer require xiabin/oauth2-sinaweibo

用例

获取token

$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

License

The MIT License (MIT). Please see License File for more information.