nocksapp/nocks-sdk-php
Composer 安装命令:
composer require nocksapp/nocks-sdk-php
包简介
PHP SDK for the Nocks API
README 文档
README
Nocks SDK PHP
Nocks SDK PHP is a php package for Nocks. It can be used in any php >=5.4.0 environment. The SDK supports both the calls to the Nocks api endpoints as well to the oauth endpoints.
Installation
This SDK uses composer.
Composer is a tool for dependency management in PHP. It allows you to declare the libraries your project depends on and it will manage (install/update) them for you.
For more information on how to use/install composer, please visit: https://github.com/composer/composer
To install this SDK into your project with composer, simply use:
$ composer require nocksapp/nocks-sdk-php
Getting Started
Oauth
The NocksOauth class provides the following methods:
- getOauthUri
- requestToken
- refreshToken
- passwordGrantToken
- scopes
- tokenScopes
Example
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 }
Please checkout the oauth docs and examples to see how the oauth methods can be used.
API
The NocksApi class provides all the Nocks resources. Please checkout the resources docs and examples.
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 }
Results
The result returned from a method call in the SDK will differ, please check the PHPDocs to see what will be returned from a method. Most of the time it will be a Model or a NocksResponse.
Model
A Model is just a simple object which holds the data returned from the api. Each Model will provide the necessary getters and setters for the specific data.
NocksResponse
A NocksResponse is an object which holds a data array and a pagination object. This is typically returned from a .find method.
Example:
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 }
Exceptions
The SDK uses the following exceptions. All exceptions are inherit from 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
- Nocks\SDK\Exception\HttpException (super)
HttpException
A HttpException is thrown when a http call to Nocks fails. The HttpException is the super exception and the actual exception that is thrown is corresponding to the http statuscode, see the documentation.
ValidationException
A ValidationException may occur when you call a function with invalid parameters or when there are missing required parameters. For example:
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 }
Note that the SDK doesn't check your request data, if there is something wrong with your data, the http call will resolve in a HttpException
ScopeConfigurationException
A ScopeConfigurationException may occur when calling a function which depends on a certain scope parameter that was not provided. For example, calling a private resource while there was no accessToken configured:
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 }
Examples
In the examples directory you will find examples for each call that is supported in this SDK.
Support
Need help or support? Please check https://www.nocks.com/support.
Found a bug? Please check the existing GitHub issues and open a new issue if necessary. Or even better, create a pull request to directly resolve the issue you have found!
nocksapp/nocks-sdk-php 适用场景与选型建议
nocksapp/nocks-sdk-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 383 次下载、GitHub Stars 达 4, 最近一次更新时间为 2015 年 09 月 10 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「api」 「payment」 「sdk」 「transaction」 「bitcoin」 「blockchain」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 nocksapp/nocks-sdk-php 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 nocksapp/nocks-sdk-php 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 nocksapp/nocks-sdk-php 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A PSR-7 compatible library for making CRUD API endpoints
A lightweight plain-PHP framework for database-backed CRUD APIs.
TrinkPOS Sanal POS (Virtual POS) API client for PHP
Modern, strongly-typed, PSR-compliant PHP client for the WeFact v2 API.
PHP SDK for the Enconvert file conversion API
支付宝开放平台v3协议文档,支持最新版PHP8+
统计信息
- 总下载量: 383
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 23
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-09-10