setono / economic-php-sdk
使用此PHP SDK来使用经济API
v0.2.1
2024-02-23 09:03 UTC
Requires
- php: >=8.1
- cuyz/valinor: ^1.9
- php-http/discovery: ^1.19
- psr/http-client: ^1.0
- psr/http-client-implementation: ^1
- psr/http-factory: ^1.0
- psr/http-factory-implementation: ^1
- psr/http-message: ^1.0 || ^2.0
- psr/log: ^1.1 || ^2.0 || ^3.0
- webmozart/assert: ^1.11
Requires (Dev)
- infection/infection: ^0.27
- nyholm/psr7: ^1.8
- phpspec/prophecy-phpunit: ^2.1
- phpunit/phpunit: ^9.6
- psalm/plugin-phpunit: ^0.18
- setono/code-quality-pack: ^2.7
- symfony/http-client: ^6.4 || ^7.0
This package is auto-updated.
Last update: 2024-09-23 10:09:49 UTC
README
在PHP中使用 E-conomic API。
安装
composer require setono/economic-php-sdk
使用方法
获取集合
<?php use Setono\Economic\Client\Client; $client = new Client('demo', 'demo'); $products = $client ->products() ->get(filter: 'name$like:b', sortBy: 'name') ; print_r($products);
将输出类似以下内容
Setono\Economic\Response\Collection\Collection Object
(
[collection] => Array
(
[0] => Setono\Economic\Response\Product\Product Object
(
[productNumber] => 2
[name] => Barred product
[salesPrice] => 50
)
[1] => Setono\Economic\Response\Product\Product Object
(
[productNumber] => 5
[name] => Fountain Pen, Blue
[salesPrice] => 30
)
[2] => Setono\Economic\Response\Product\Product Object
(
[productNumber] => 1
[name] => Noname T-shirt Black
[salesPrice] => 70
)
[3] => Setono\Economic\Response\Product\Product Object
(
[productNumber] => 3
[name] => SIlk Fabric
[salesPrice] => 50
)
)
[pagination] => Setono\Economic\Response\Pagination\Pagination Object
(
[maxPageSizeAllowed] => 1000
[skipPages] => 0
[pageSize] => 20
[results] => 4
[resultsWithoutFilter] => 7
[firstPage] => Setono\Economic\Response\Pagination\Page Object
(
[endpoint] => products
[skipPages] => 0
[pageSize] => 20
[url] => https://restapi.e-conomic.com/products?skippages=0&pagesize=20&filter=name%24like%3Ab&sort=name
)
[lastPage] => Setono\Economic\Response\Pagination\Page Object
(
[endpoint] => products
[skipPages] => 0
[pageSize] => 20
[url] => https://restapi.e-conomic.com/products?skippages=0&pagesize=20&filter=name%24like%3Ab&sort=name
)
[nextPage] =>
)
)
分页
<?php use Setono\Economic\Client\Client; $client = new Client('demo', 'demo'); $skipPages = 0; do { $products = $client ->products() ->get(skipPages: $skipPages++); } while(!$products->isEmpty());
其他请求
如果您要调用的端点或方法尚不存在,您有两个选择:1) 创建一个PR并添加缺少的部分或2) 使用通用的 request
方法
<?php use Setono\Economic\Client\Client; require_once '../vendor/autoload.php'; $client = new Client('API_KEY', 'API_SECRET'); /** @var \Psr\Http\Message\ResponseInterface $response */ $response = $client->request(/** @var \Psr\Http\Message\RequestInterface $request */ $request);
生产使用
内部,此库使用CuyZ/Valinor库,该库特别适合将API响应转换为DTO。然而,此库有一些开销,并且最好在启用缓存的情况下使用。
在实例化 Client
时,您可以提供一个 MapperBuilder
实例。利用这个机会来设置缓存
<?php use CuyZ\Valinor\Cache\FileSystemCache;use CuyZ\Valinor\MapperBuilder;use Setono\Economic\Client\Client;use Setono\Economic\DTO\Box; require_once '../vendor/autoload.php'; $cache = new FileSystemCache('path/to/cache-directory'); $client = new Client('API_KEY', 'API_SECRET', (new MapperBuilder())->withCache($cache));
您可以在这里了解更多信息:Valinor: 性能和缓存。