los/api-client

使用Hal的REST API客户端


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');