zooxsmart / los-api-client
使用 Hal 的 REST API 客户端
2.7.3
2024-03-04 12:39 UTC
Requires
- php: ^8.1
- ext-json: *
- ext-mbstring: *
- guzzlehttp/guzzle: ^7.8
- guzzlehttp/psr7: ^2.6
- laminas/laminas-eventmanager: ^3.12
- laminas/laminas-paginator: ^2.18
- psr/container: ^1.0 || ^2.0
- psr/link: ^1.0
- psr/simple-cache: ^1.0
- ramsey/uuid: ^4.7
Requires (Dev)
- doctrine/coding-standard: ^12.0.0
- phpstan/phpstan: ^1.10
- squizlabs/php_codesniffer: ^3.7.2
Suggests
- zooxsmart/los-log: zooxsmart/los-log for logging
- dev-master
- 2.7.x-dev
- 2.7.3
- 2.7.2
- 2.7.1
- 2.7.0
- 2.6.x-dev
- 2.5.x-dev
- 2.5.0
- 2.4.x-dev
- 2.4.1
- 2.4.0
- 2.3.x-dev
- 2.3.2
- 2.3.1
- 2.3.0
- 2.2.x-dev
- 2.2.1
- 2.2.0
- 2.1.3
- 2.1.2
- 2.1.1
- 2.1.0
- 2.0.1
- 2.0.0
- 1.1.0
- 1.0.6
- 1.0.5
- 1.0.4
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
- 0.9.2
- 0.9.1
- 0.9.0
- 0.8.0
- 0.7.0
- 0.6.0
- 0.5.0
- 0.4.0
- 0.3.0
- 0.2.0
- 0.1.0
- dev-abner/test
- dev-release/portal-resiliente
- dev-feature/parametro-save-cache
- dev-feature/default-cache-key
- dev-feature/request-na-exception
- dev-php-8
- dev-2.3.x-merge-up-into-2.4.x_6005aa7d441dd6.04266332
- dev-2.3.x-merge-up-into-2.4.x_6000159b3e83e9.95835183
- dev-request-pre-event
- dev-toArray-without-links
- dev-add-without-header
- dev-2.2.x-merge-up-into-2.3.x_5fd21b8f636e06.44005561
- dev-without-header
- dev-clear-cache-key
- dev-develop
This package is auto-updated.
Last update: 2024-09-24 20:09:41 UTC
README
ApiClient 是一个 PHP 库,用于使用 Hal 消费 Restful API,类似于 Apigility。
要求
请查看 composer.json
安装
php composer.phar require los/api-client
配置
您至少需要配置 Api URI。
如果您使用实现了 container-interopt
的框架,可以使用以下配置
将此模块中的 los-api-client.global.php.dist 复制到您的应用程序配置文件夹,并进行必要的更改。
'los' => [ 'api-client' => [ 'root_uri' => 'https://:8000', 'add_request_id' => true, 'add_request_time' => true, 'add_request_depth' => true, 'headers' => [ 'Accept' => 'application/hal+json', 'Content-Type' => 'application/json', ], 'query' => [ 'key' => '123', ], 'request_options' => [ 'request_options' => [ 'timeout' => 10, 'connect_timeout' => 2, 'read_timeout' => 10, ], ], 'default_ttl' => 600, ], ],
用法
创建客户端
您可以使用上述配置的 Los\ApiClient\ClientFactory
或手动进行
$client = new \Los\ApiClient\ApiClient('http://api.example.com');
单个资源
/* @var \Los\ApiClient\ApiClient $client */ $client = new \Los\ApiClient\ApiClient('http://api.example.com'); /* @var \Los\ApiClient\Resource\ApiResource $ret */ $ret = $client->get('/album/1'); // $data is an array with all data and resources (_embedded) from the response $data = $ret->getData();
集合
/* @var \Los\ApiClient\ApiClient $client */ $client = new \Los\ApiClient\ApiClient('http://api.example.com'); /* @var \Los\ApiClient\Resource\ApiResource $ret */ $ret = $client->get('/album', [ 'query' => ['year' => 2018] ]); // $data is an array with all data and resources (_embedded) from the response $data = $ret->getData(); // $data is an array with the first album resource from the response $data = $ret->getFirstResource('album'); // $data is an array with the all album resources from the response $data = $ret->getResources('album'); // $data is an array with the all resources from the response $data = $ret->getResources();
缓存
/* @var \Psr\SimpleCache\CacheInterface */ $cache = null; // Any PSR-16 cache service. /* @var \Los\ApiClient\ApiClient $client */ $client = new \Los\ApiClient\ApiClient('http://api.example.com', $cache); // The last param is a per item ttl, please make sure your cache service can handle it. /* @var \Los\ApiClient\Resource\ApiResource $ret */ $ret = $client->getCached('/album', 'cached-key', [ 'query' => ['year' => 2018] ], null); // $data is an array with all data and resources (_embedded) from the response $data = $ret->getData(); // $data is an array with the first album resource from the response $data = $ret->getFirstResource('album'); // $data is an array with the all album resources from the response $data = $ret->getResources('album'); // $data is an array with the all resources from the response $data = $ret->getResources();
事件
客户端触发一些事件
- request.pre
- request.post
- request.fail
有关事件的更多信息,请参阅 zend-eventmanager。
请求 ID
客户端会自动为每个请求添加一个 X-Request-Id,但前提是没有添加之前的 X-Request-Id。
您可以使用以下方式强制使用新 ID
$client = $this->getServiceLocator()->get('hermes'); $client->addRequestId(); // Auto generared $client->addRequestId('123abc');