thinkerytim / oauth2-flexmls
为PHP League的OAuth 2.0客户端提供FlexMLS OAuth 2.0支持
v0.1-alpha
2020-06-20 15:57 UTC
Requires
- php: >=7.2
- league/oauth2-client: ^2.0
Requires (Dev)
- mockery/mockery: ^1.4
- phpunit/phpunit: ^9.2
This package is auto-updated.
Last update: 2024-09-20 05:46:27 UTC
README
此包为PHP League的OAuth 2.0客户端提供FlexMLS OAuth 2.0支持。用户可以对FlexMLS Spark平台(https://www.sparkplatform.com)进行身份验证,并获得用于Spark API或FlexMLS RESO Web API的令牌。
使用这些令牌,开发者可以为网页、移动或数据复制创建房地产应用程序。
安装
composer require thinkerytim/oauth2-flexmls
用法
$flexmlsProvider = new \ThinkeryTim\OAuth2\Client\Provider\FlexMLS([ 'clientId' => 'yourId', // The client ID assigned to you by the Spark Platform 'clientSecret' => 'yourSecret', // The client password assigned to you by the Spark Platform 'redirectUri' => 'yourRedirectUri' // The return URL you specified for your app on Spark Platform ]); // Get authorization code if (!isset($_GET['code'])) { // Options are optional, defaults to 'openid' only $options = ['scope' => 'openid']; // Get authorization URL $authorizationUrl = $flexmlsProvider->getAuthorizationUrl($options); // Get state and store it to the session $_SESSION['oauth2state'] = $flexmlsProvider->getState(); // Redirect user to authorization URL header('Location: ' . $authorizationUrl); exit; // Check for errors } elseif (empty($_GET['state']) || (isset($_SESSION['oauth2state']) && $_GET['state'] !== $_SESSION['oauth2state'])) { if (isset($_SESSION['oauth2state'])) { unset($_SESSION['oauth2state']); } exit('Invalid state'); } else { // Get access token try { $accessToken = $flexmlsProvider->getAccessToken( 'authorization_code', [ 'code' => $_GET['code'] ] ); } catch (\League\OAuth2\Client\Provider\Exception\IdentityProviderException $e) { exit($e->getMessage()); } // Get resource owner try { $resourceOwner = $flexmlsProvider->getResourceOwner($accessToken); } catch (\League\OAuth2\Client\Provider\Exception\IdentityProviderException $e) { exit($e->getMessage()); } // Now you can store the results to session etc. $_SESSION['accessToken'] = $accessToken; $_SESSION['resourceOwner'] = $resourceOwner; var_dump( $resourceOwner->getId(), $resourceOwner->getName(), $resourceOwner->getPrimaryEmail(), $resourceOwner->OfficeMlsId, // this is using the _get magic method $resourceOwner->toArray() ); }
有关更多信息,请参阅PHP League的一般用法示例。
测试
$ ./vendor/bin/phpunit
许可证
MIT许可证(MIT)。请参阅许可证文件以获取更多信息。