global-exam / api-sdk-php
GlobalExam API PHP SDK
此包的官方仓库似乎已不存在,因此该包已被冻结。
Requires
- guzzlehttp/guzzle: 6.3.*
Requires (Dev)
- mockery/mockery: 1.0.*
- phpunit/phpunit: 5.7.*
This package is not auto-updated.
Last update: 2022-06-23 01:47:06 UTC
README
要求
- PHP >= 5.6
- cURL 库已启用
安装
Composer
composer require global-exam/api-sdk-php:dev-master
使用
作为访客
use GlobalExam\Api\Sdk\Api; $api = new Api();
需要令牌的访客
use GlobalExam\Api\Sdk\Api; use GlobalExam\Api\Sdk\Authentication\OAuthClient; use GlobalExam\Api\Sdk\Authentication\PasswordCredentialsGrant; $oauthClient = new OAuthClient('clientId', 'clientSecret'); $authenticator = new PasswordCredentialsGrant($oauthClient, 'email@domain.com', 'password', '', [ 'Accept-Language' => 'fr', 'X-Subdomain' => 'your-organization-subdomain', 'X-Agent-Country' => 'fr', 'X-Forwarded-For' => '0.0.0.0', ]); $api = new Api($authenticator); // Keep tokens for future calls $tokens = $api->login();
已知用户
use GlobalExam\Api\Sdk\Api; use GlobalExam\Api\Sdk\Authentication\AuthorizationCodeGrant; $tokens = json_decode($tokens->getBody()->getContents(), true); // Play authenticated calls $authenticator = new AuthorizationCodeGrant($oauthClient, $tokens, '', [ 'Accept-Language' => 'fr', 'X-Subdomain' => 'your-organization-subdomain', 'X-Agent-Country' => 'fr', 'X-Forwarded-For' => '0.0.0.0', ]); $api = new Api($authenticator); $api->login(); $response = $api->user()->me(); // ResponseInterface
作为服务器
use GlobalExam\Api\Sdk\Api; use GlobalExam\Api\Sdk\Authentication\ClientCredentialsGrant; $oauthClient = new OAuthClient('clientId', 'clientSecret'); $authenticator = new ClientCredentialsGrant($oauthClient); $api = new Api($authenticator); $tokens = $api->login(); $tokens = json_decode($tokens->getBody()->getContents(), true); $api->setAuthenticator(new AuthorizationCodeGrant($oauthClient, $tokens));
处理令牌过期
您可能会收到一个来自 Guzzle 异常 的 401 未授权 响应。
尝试根据当前凭据获取另一个访问令牌。
try { $api->user()->me(); } catch (\GuzzleHttp\Exception\ClientException $e) { // Expired token if ($e->getResponse()->getStatusCode() === 401) { // Ask new credentials based on the current refresh token $tokens = $api->user()->oauth()->renewToken(); $tokens = json_decode($tokens->getBody()->getContents(), true); // Set a fresh authenticator $api->setAuthenticator(new AuthorizationCodeGrant($oauthClient, $tokens, '', [ 'Accept-Language' => 'fr', 'X-Subdomain' => 'your-organization-subdomain', 'X-Agent-Country' => 'fr', 'X-Forwarded-For' => '0.0.0.0', ]); $response = $api->user()->me(); $response->getStatusCode(); // 200 } }
API 参考
constructor
描述
创建一个新的 Api 实例。
Api ( AuthenticationInterface $authenticator = null ): Api
输入
authenticator
有效的认证器。
输出
返回 Api 实例。
示例
$oauthClient = new OAuthClient('clientId', 'clientSecret'); $authenticator = new PasswordCredentialsGrant($oauthClient, 'email@domain.com', 'password', '', [ 'Accept-Language' => 'fr', 'X-Subdomain' => 'your-organization-subdomain', 'X-Agent-Country' => 'fr', 'X-Forwarded-For' => '0.0.0.0', ]); $api = new Api($authenticator);
setBaseUrl
描述
设置另一个基本 URL。
setBaseUrl ( string $baseUrl ): Api
输入
baseUrl
有效的 URL。
输出
返回 Api 实例。
示例
$api = new Api(); $api->setBaseUrl('http://api.global-exam.io');
setAuthenticator
描述
设置另一个认证器。
setAuthenticator ( AuthenticationInterface $authenticator ): Api
输入
认证器
实现了 AuthenticationInterface 的任何类。预构建的类包括
PasswordCredentialsGrantAuthorizationCodeGrantClientCredentialsGrant
输出
返回 Api 实例。
示例
$api = new Api(); $oauthClient = new OAuthClient('clientId', 'clientSecret'); $authenticator = new PasswordCredentialsGrant($oauthClient, 'email@domain.com', 'password', '', [ 'Accept-Language' => 'fr', 'X-Subdomain' => 'your-organization-subdomain', 'X-Agent-Country' => 'fr', 'X-Forwarded-For' => '0.0.0.0', ]); $api->setAuthenticator($authenticator);
login
描述
表示您想要进行认证。
这将登录用户,如果您指定了 OAuthPasswordGrant 认证器,则返回凭证。
login ( ): Api
输出
如果使用了 OAuthPasswordGrant 认证器,则返回作为 ResponseInterface 的凭证。否则返回当前的 Api 实例。
示例
$oauthClient = new OAuthClient('clientId', 'clientSecret'); $authenticator = new PasswordCredentialsGrant($oauthClient, 'email@domain.com', 'password', '', [ 'Accept-Language' => 'fr', 'X-Subdomain' => 'your-organization-subdomain', 'X-Agent-Country' => 'fr', 'X-Forwarded-For' => '0.0.0.0', ]); $api = new Api($authenticator); // Keep credentials for future calls $tokens = $api->login(); // Or $oauthClient = new OAuthClient('clientId', 'clientSecret'); $authenticator = new AuthorizationCodeGrant($oauthClient, $tokens, '', [ 'Accept-Language' => 'fr', 'X-Subdomain' => 'your-organization-subdomain', 'X-Agent-Country' => 'fr', 'X-Forwarded-For' => '0.0.0.0', ]); $api = new Api($authenticator); $api->login();
logout
描述
禁用未来的调用认证。这将不会注销当前用户。请参阅用户资源。
logout ( $clearCredentials = false ): Api
输入
clearCredentials
如果为 true,则也会清除当前认证器。
输出
返回 Api 实例。
示例
$oauthClient = new OAuthClient('clientId', 'clientSecret'); $authenticator = new AuthorizationCodeGrant($oauthClient, $tokens, '', [ 'Accept-Language' => 'fr', 'X-Subdomain' => 'your-organization-subdomain', 'X-Agent-Country' => 'fr', 'X-Forwarded-For' => '0.0.0.0', ]); $api = new Api($authenticator); $api->login(); // Perform actions $api->logout(true);
clearCredentials
描述
清除当前认证器。
clearCredentials ( ): Api
输出
返回 Api 实例。
示例
$oauthClient = new OAuthClient('clientId', 'clientSecret'); $authenticator = new AuthorizationCodeGrant($oauthClient, $tokens, '', [ 'Accept-Language' => 'fr', 'X-Subdomain' => 'your-organization-subdomain', 'X-Agent-Country' => 'fr', 'X-Forwarded-For' => '0.0.0.0', ]); $api = new Api($authenticator); $api->login(); // Perform actions $api->logout(); $api->clearCredentials();
isAuthenticated
描述
了解您是否已经认证。
isAuthenticated ( ): bool
输出
返回 true 或 false。
示例
$oauthClient = new OAuthClient('clientId', 'clientSecret'); $authenticator = new AuthorizationCodeGrant($oauthClient, $tokens, '', [ 'Accept-Language' => 'fr', 'X-Subdomain' => 'your-organization-subdomain', 'X-Agent-Country' => 'fr', 'X-Forwarded-For' => '0.0.0.0', ]); $api = new Api($authenticator); $api->login(); $api->isAuthenticated(); // true $api->logout(); $api->isAuthenticated(); // false
send
描述
对 API 执行 HTTP 请求。
大多数情况下,您不会使用此方法,因为存在公共方法可以访问给定资源。
send ( string $method, string $uri, array $body = [], array $params = [], array $headers = [] ): ResponseInterface
输入
method
HTTP 动词,如 GET、POST 等。
uri
资源的路径,将添加到基础 URL。
body
任何将要被JSON编码并附加到请求中的数组。
params
任何将要被转换成查询字符串并添加到URI的数组。
headers
额外的头部信息。
输出
返回一个实现PSR-7 ResponseInterface接口的响应。
示例
$oauthClient = new OAuthClient('clientId', 'clientSecret'); $authenticator = new AuthorizationCodeGrant($oauthClient, [ 'access_token' => 'a', 'refresh_token' => 'b', 'expires_in' => 1, ], '', [ 'Accept-Language' => 'fr', 'X-Subdomain' => 'your-organization-subdomain', 'X-Agent-Country' => 'fr', 'X-Forwarded-For' => '0.0.0.0', ]); $api = new Api($authenticator); $response = $api->send('GET', '/user/me'); // $response->getStatusCode() // $response->getBody()->getContents()
资源
资源允许您快速响应,而不是自行构建请求。
浏览src/Resource文件夹以查看可用的资源。
示例
$api->user()->me(); $api->auth()->register(array $body);
测试
在项目目录下
composer install来获取phpunitphpunit来运行测试