setono/economic-php-sdk
最新稳定版本:v0.2.1
Composer 安装命令:
composer require setono/economic-php-sdk
包简介
Consume the Economic API with this PHP SDK
README 文档
README
Consume the E-conomic API in PHP.
Installation
composer require setono/economic-php-sdk
Usage
Get a collection
<?php use Setono\Economic\Client\Client; $client = new Client('demo', 'demo'); $products = $client ->products() ->get(filter: 'name$like:b', sortBy: 'name') ; print_r($products);
will output something like:
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] =>
)
)
Paginate
<?php use Setono\Economic\Client\Client; $client = new Client('demo', 'demo'); $skipPages = 0; do { $products = $client ->products() ->get(skipPages: $skipPages++); } while(!$products->isEmpty());
Other requests
If the endpoint or method you want to call isn't present yet, you have two options: 1) Create a PR and add the missing parts or 2) use the generic request method:
<?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);
Production usage
Internally this library uses the CuyZ/Valinor library which is particularly well suited for turning API responses in DTOs. However, this library has some overhead and works best with a cache enabled.
When you instantiate the Client you can provide a MapperBuilder instance. Use this opportunity to set a cache:
<?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));
You can read more about it here: Valinor: Performance and caching.
统计信息
- 总下载量: 995
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-05-24