nocksapp/nocks-sdk-php

Nocks API 的 PHP SDK

v2.1.0 2019-05-02 10:53 UTC

README

Latest Stable Version Total Downloads Latest Unstable Version

Nocks SDK PHP

Nocks SDK PHP 是一个用于 Nocksphp 包。它可以在任何 >=5.4.0 的 PHP 环境中使用。该 SDK 支持对 Nocks api 端点的调用以及 oauth 端点的调用。

安装

此 SDK 使用 composer。

Composer 是 PHP 中的依赖管理工具。它允许您声明项目所依赖的库,并且会为您管理(安装/更新)它们。

有关如何使用/安装 composer 的更多信息,请访问: https://github.com/composer/composer

要使用 composer 将此 SDK 安装到您的项目中,请简单使用

$ composer require nocksapp/nocks-sdk-php

入门

OAuth

NocksOauth 类提供以下方法

  • getOauthUri
  • requestToken
  • refreshToken
  • passwordGrantToken
  • scopes
  • tokenScopes

示例

require '../../vendor/autoload.php';

use Nocks\SDK\NocksOauth;
use Nocks\SDK\Constant\Platform;

try {
    $clientId = '1';
    $clientSecret = '1234';
    $scopes = ['user.read'];
    $redirectUri = 'https://www.example.com';

    $nocksOauth = new NocksOauth(Platform::SANDBOX, $clientId, $clientSecret, $scopes, $redirectUri);
    $uri = $nocksOauth->getOauthUri();

    // For example, redirect the user to the Nocks login page
    header('Location: ' . $uri);
    die();
} catch (\Nocks\SDK\Exception\Exception $e) {
    // Handle any SDK exception
}

请查看 oauth 文档示例,了解如何使用 oauth 方法。

API

NocksApi 类提供所有 Nocks 资源。请查看 资源文档示例

require '../../vendor/autoload.php';

use Nocks\SDK\NocksApi;
use Nocks\SDK\Constant\Platform;

try {
    $accessToken = 'your_access_token';
    $nocksApi = new NocksApi(Platform::SANDBOX, $accessToken);

    $user = $nocksApi->user->findAuthenticated();
} catch (\Nocks\SDK\Exception\Exception $e) {
    // Handle any SDK exception
}

结果

SDK 中方法调用返回的结果可能会有所不同,请检查 PHPDocs 以查看方法将返回什么。大多数情况下,它将是一个 Model 或一个 NocksResponse

模型

Model 只是一个简单的 object,它包含从 api 返回的 data。每个 Model 都将提供必要的 getterssetters 以访问特定的 data

NocksResponse

NocksResponse 是一个对象,它包含一个 data array 和一个 pagination object。这通常是 .find 方法返回的。

示例

require '../../vendor/autoload.php';

use Nocks\SDK\NocksApi;
use Nocks\SDK\Constant\Platform;

try {
    $accessToken = 'your_access_token';
    $nocksApi = new NocksApi(Platform::SANDBOX, $accessToken);

    $result = $nocksApi->transaction->find();
    $result->getPagination(); // Do something with pagination
	
    // Loop through transactions
    foreach ($result->getData() as $transaction) {
        // Do something with the transaction
    }	
} catch (\Nocks\SDK\Exception\Exception $e) {
    // Handle any SDK exception
}

异常

SDK 使用以下异常。所有异常都继承自 Nocks\SDK\Exception\Exception

  • Nocks\SDK\Exception\Exception (super)
    • Nocks\SDK\Exception\HttpException (super)
      • Nocks\SDK\Exception\BadRequestException
      • Nocks\SDK\Exception\ForbiddenException
      • Nocks\SDK\Exception\GoneException
      • Nocks\SDK\Exception\InternalServerError
      • Nocks\SDK\Exception\MethodNotAllowedException
      • Nocks\SDK\Exception\NotAcceptable
      • Nocks\SDK\Exception\NotFoundException
      • Nocks\SDK\Exception\ServiceUnavailable
      • Nocks\SDK\Exception\TooManyRequests
      • Nocks\SDK\Exception\UnauthorizedException
    • Nocks\SDK\Exception\ValidationException
    • Nocks\SDK\Exception\ScopeConfigurationException

HttpException

当对 Nocks 进行 HTTP 调用失败时,会抛出 HttpException。HttpException 是 super 异常,实际抛出的异常与 http statuscode 相对应,请参阅 文档

ValidationException

在调用具有无效参数的函数或存在缺失的必需参数时,可能会发生 ValidationException。例如

use Nocks\SDK\NocksApi;
use Nocks\SDK\Constant\Platform;

try {
    $accessToken = 'your_access_token';
    $nocksApi = new NocksApi(Platform::SANDBOX, $accessToken);

    $userToUpdate = new User();
    // $userToUpdate->setUuid('1234'); Will occur in an exception when not set
    $user = $nocksApi->user->update($userToUpdate);
} catch (\Nocks\SDK\Exception\ValidationException $e) {
    // A ValidationException will be thrown when the $userToUpdate has no uuid
}

请注意,SDK不会检查您的请求数据,如果您的数据存在问题,HTTP调用将解析为HttpException。

ScopeConfigurationException

当调用依赖于未提供的特定作用域参数的函数时,可能会发生ScopeConfigurationException。例如,在没有配置accessToken的情况下调用private资源。

use Nocks\SDK\NocksApi;
use Nocks\SDK\Constant\Platform;

try {
    $accessToken = null;
    $nocksApi = new NocksApi(Platform::SANDBOX, $accessToken);

    $userToUpdate = new User([
        'uuid' => '1234',
        'locale' => 'nl_NL',
    ]);
    $user = $nocksApi->user->update($userToUpdate);
} catch (\Nocks\SDK\Exception\ScopeConfigurationException $e) {
    // A ScopeConfigurationException will be thrown when the $accessToken is null
}

示例

示例目录中,您将找到该SDK支持的所有调用的示例。

支持

需要帮助或支持?请访问https://www.nocks.com/support

发现错误?请检查现有的GitHub问题,并在必要时打开新的问题。或者更好的是,创建一个pull request以直接解决您发现的问题!