los/los-api-client

使用 Hal 的 REST API 客户端

1.0.3 2015-12-05 13:52 UTC

This package is auto-updated.

Last update: 2024-08-26 03:13:49 UTC


README

Build Status

LosApiClient 是一个库,用于使用 Hal(JSON 或 XML)来消费 Restful API,类似于 Apigility

可以与纯 PHP 或任何 PHP 框架一起使用,例如 ZendFramework 2 和 Zend-Expressive。

要求

  • PHP >= 5.6
  • Zend Http >= 2.4
  • Zend ModuleManager >= 2.5
  • Zend ServiceManager >= 2.5
  • nocarrier/hal >= 0.9

安装

使用 composer(推荐)

php composer.phar require los/los-api-client

配置

您需要至少配置 Api URI。

如果使用实现 container-interopt 的框架,您可以使用以下配置

将此模块中的 los-api-client.global.php.dist 复制到您的应用程序配置文件夹,并做出必要的更改。

有关 http-client 选项的更多信息,请参阅官方文档 Zend\Http\Client 选项

'los_api_client' => [
    'uri' => 'https://:8000',
    'depth' => 0,
    'http_client' => [
        'options' => [
            'timeout'       => 60,
            'sslverifypeer' => false,
            'keepalive'     => true,
            'adapter'       => 'Zend\Http\Client\Adapter\Socket',
        ],
    ],
    'headers' => [
        'Accept'       => 'application/hal+json',
        'Content-Type' => 'application/json',
    ],
]

使用

创建客户端

您可以使用 LosApiClient\Api\ClientFactory 使用上述配置或手动操作

$httpClient = new Zend\Http\Client('http://127.0.0.1', []);
$client = new LosapiClient\Apt\Client($httpClient, 10);

注入 Cerberus 断路器

您可以使用断路器与客户端一起使用,以控制失败和成功,并避免不必要的尝试。

有关 Cerberus 的更多信息,请参阅它的 自己的仓库

$httpClient = new Zend\Http\Client('http://127.0.0.1', []);
$storage = Zend\Cache\StorageFactory\StorageFactory::factory([
            'adapter' => [
                'name' => 'memory',
                'options' => [
                    'namespace' => 'my-service',
                ],
            ],
            'plugins' => [
                'exception_handler' => [
                    'throw_exceptions' => false,
                ],
            ],
        ]);
$cerberus = new Cerberus($storage, 2, 2);

// Create a new client
$client = new LosapiClient\Apt\Client($httpClient, 10, $cerberus, 'my-service);

// Or add it to a previously created client 
$client->setCircuitBreaker($cerberus);
$client->setServiceName('my-service');

单个资源

/* @var \LosApiClient\Api\Client $client */
$client = $this->getServiceLocator()->get('los.api.client');
/* @var \LosApiClient\Resource\Resource $ret */
$ret = $client->get('/album/1');

// $data is an array with all data and resources (_embedded) from the response
$data = $ret->getData();

// $data is an array only with data from the response
$data = $ret->getData(false);

集合

/* @var \LosApiClient\Api\Client $client */
$client = $this->getServiceLocator()->get('los.api.client');

// Setting depth of _embedded resources to 10
$client->setDepth(10);

/* @var \LosApiClient\Resource\Resource $ret */
$ret = $client->get('/album',['year' => 2015]);

// $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 \LosApiClient\Api\Client $client */
$client = $this->getServiceLocator()->get('los.api.client');
/* @var \LosApiClient\Resource\Resource $ret */
$ret = $client->get('/album',['year' => 2015]);

// Returns how many items a page can have
$ret->getPaginator()->getPageSize();

// Returns how many pages the response has
$ret->getPaginator()->getPageCount();

// Returns how many items the response has (across all pages)
$ret->getPaginator()->getTotalItems();

// Returns the current page
$ret->getPaginator()->getPage();

您可以轻松地遍历页面

/* @var \LosApiClient\Api\Client $client */
$client = $this->getServiceLocator()->get('los.api.client');
$page = 1;
do {
    /* @var \LosApiClient\Resource\Resource $ret */
    $ret = $client->get('/album',[
        'year' => 2015,
        'page' => $page;
    ]);
    $data = $ret->getData();
    $page++;
} while ($ret->getPaginator()->hasMorePages());