webleit / revisoapi
Reviso Api - PHP SDK
Requires
- php: ^8.0
- ext-json: *
- illuminate/collections: ^8.0 || ^9.0
- php-http/discovery: ^1.0
- php-http/httplug: ^2.0
- php-http/message-factory: ^1.0
- psr/http-client-implementation: ^1.0
- psr/http-message: ^1.0
Requires (Dev)
- ext-curl: *
- dms/phpunit-arraysubset-asserts: ^0.3.1
- friendsofphp/php-cs-fixer: ^3.5
- guzzlehttp/psr7: ^2.1
- php-http/curl-client: ^2.2
- php-http/message: ^1.12
- php-http/mock-client: ^1.0
- phpstan/phpstan: ^1.4
- phpunit/phpunit: ^9.0
- rector/rector: ^0.12.15
This package is auto-updated.
Last update: 2024-09-09 06:44:29 UTC
README
这个库是PHP的一个SDK,简化了使用Reviso REST API(http://api-docs.reviso.com)的过程。
它提供了一个接口,简化了与API的交互,无需担心实际的REST请求,同时使用非常简单的模型类来包装各种响应,这些模型类可以与任何其他库或框架一起使用。
安装
composer require webleit/revisoapi
HTTP客户端
为了与Reviso API通信,您需要一个HTTP客户端库。从该包的v2版本开始,HTTP客户端不再包含在内,让您可以选择您最喜欢的客户端,避免任何潜在的依赖冲突。
您可以使用任何实现php-http/client-implementation的库来发送HTTP消息。
以下是所有官方支持的客户端和适配器的列表:http://docs.php-http.org/en/latest/clients.html
您可以在HTTPlug文档中了解更多信息。
使用方法
要使用库,只需引入composer的autoload文件,然后启动库本身。
require './vendor/autoload.php'; $reviso = new \Webleit\RevisoApi\Reviso($appSecretToken, $agreementGrantToken);
这样,库将尝试找到您可能已经拥有的任何HTTP客户端实现。如果您想,您可以像这样将特定的HttpClient实例传递给库
require './vendor/autoload.php'; $reviso = new \Webleit\RevisoApi\Reviso($appSecretToken, $agreementGrantToken, $yourHttpClient);
如果您想使用演示账户,只需不要指定认证参数,您就可以使用任何GET请求。
$reviso = new \Webleit\RevisoApi\Reviso();
API调用
要调用任何API,只需使用在API文档中报告的相同名称。您可以使用getEndpoints()方法获取支持API的列表
$reviso->getEndpoints();
例如,您可以通过以下方式获取客户列表
$customers = $reviso->customers->get();
或客户组列表
$groups = $reviso->customerGroups->get();
列表调用
要从一个模块中获取资源列表,请使用get()方法
$customers = $reviso->customers->get();
要导航页面,请使用"page"和"perPage"方法
$customers = $reviso->customers->page(1)->perPage(100)->get();
过滤器
要过滤模块中的资源列表,请在调用get()之前使用where()方法
$customers = $reviso->customers->where('corporateIdentificationNumber', '=', '123456789')->get();
要导航页面,请使用"page"和"perPage"方法
$customers = $reviso->customers->page(1)->perPage(100)->get();
返回类型
任何"list" API调用都返回一个Collection对象,该对象包含有关列表本身的信息,允许进一步分页,并将项目列表存储在Laravel Collection包(Illuminate\Support\Collection
)中。因此,您可以将结果用作Collection,允许映射、缩减、序列化等。
$customers = $reviso->customers->get(); $data = $customers->toArray(); $json = $customers->toJson(); // After fetch filtering in php $filtered = $customers->where('accountNumber', '>', 200); // Grouping $filtered = $customers->groupBy('country');
任何"resource" API调用都返回一个类对象,该类是专门针对您正在获取的单个资源。例如,调用
$customer = $reviso->customers->get($accuntNumber); $data = $customer->toArray(); $name = $customer->name;
将返回一个\Weble\RevisoApi\Model
对象,该对象是可数组的,并且是可序列化的,因此可以以多种方式使用。
CRUD
您可以从Endpoint类或模型本身创建/读取/更新/删除资源。
创建
$data = [ /** Data of the customer */ ]; $customer = $reviso->customers->create($data);
读取
$customer = $reviso->customers->find($accountNumber);
更新
$data = [ /** Data of the customer */ ]; $customer = $reviso->customers->find($accountNumber); $customer->save($data);
删除
$data = [ /** Data of the customer */ ]; $customer = $reviso->customers->find($accountNumber); $customer->delete();
测试
本软件包包含一些测试用例,用于测试软件包的基本功能。为了在“CRUD”方法上运行测试,您需要在"tests/
目录下创建一个config.json
文件,包含您想用作测试基础的应用的认证详情。您可以将其从同一目录下的config.example.json
复制过来。
composer test
从V1升级到V2
贡献
发现错误、发送拉取请求或改进文档——任何贡献都受到欢迎并非常感谢
版本控制
使用语义版本控制规范(SemVer)。
版权和许可
版权属于Weble Srl,根据MIT许可证发布。