los/los-api-client
Composer 安装命令:
composer require los/los-api-client
包简介
REST API Client using Hal
README 文档
README
LosApiClient is a library to consume Restful APIs using Hal (json or xml) like Apigility.
Can be used with pure PHP or any PHP framework, like ZendFramework 2 and Zend-Expressive.
Requirements
- PHP >= 5.6
- Zend Http >= 2.4
- Zend ModuleManager >= 2.5
- Zend ServiceManager >= 2.5
- nocarrier/hal >= 0.9
Installation
Using composer (recommended)
php composer.phar require los/los-api-client
Configuration
You need to configure at least the Api URI.
If using a framework that implements container-interopt, you can use the following configuration:
Copy the los-api-client.global.php.dist from this module to your application's config folder and make the necessary changes.
For more information about the http-client options, please check the official documentation at Zend\Http\Client options.
'los_api_client' => [ 'uri' => 'https://localhost: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', ], ]
Usage
Creating the client
You can use the LosApiClient\Api\ClientFactory usign the above configuration or manually:
$httpClient = new Zend\Http\Client('http://127.0.0.1', []); $client = new LosapiClient\Apt\Client($httpClient, 10);
Injecting a Cerberus Circuit Breaker
You can use the client with a circuit breaker to control failures and success and avoid uncessary attempts.
More information about cerberus on it's own repository.
$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');
Single resource
/* @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);
Collection
/* @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();
Paginator
This module provides a paginator helper.
/* @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();
You can easily loop through the pages:
/* @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());
los/los-api-client 适用场景与选型建议
los/los-api-client 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 437 次下载、GitHub Stars 达 3, 最近一次更新时间为 2015 年 10 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「rest」 「api」 「client」 「zf2」 「hal」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 los/los-api-client 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 los/los-api-client 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 los/los-api-client 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A PSR-7 compatible library for making CRUD API endpoints
Api bundle
Mock PSR-18 HTTP client
Asynchronous MQTT client built on React
Client for Google Directions API to add interpolated points to a route consisting of given coordinates.
A Flickr wrapper to allow you to call the Flickr api with Guzzle as the backend.Goal is to have 100% Flickr api coverage rather than just upload/display photos (currently at 23%).
统计信息
- 总下载量: 437
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 6
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-10-24