zendframework/zendservice-api
最新稳定版本:2.0.0
Composer 安装命令:
composer require zendframework/zendservice-api
包简介
Micro framework for HTTP API client
关键字:
README 文档
README
Abandoned
This component is abandoned and no longer maintained. We recommend using Guzzle (which has middleware features that enable API access), and/or tools built on Guzzle (e.g., Uhura) that provide generic API access features.
zendservice-api is a micro HTTP framework to consume generic API calls in PHP. This framework can be used to create PHP libraries that consume specific HTTP API using simple configuration array (or files).
This project uses the zend-http component of Zend Framework.
Installation
You can install this component using composer with following commands:
composer require zendframework/zendservice-api
Usage
The ZendService\Api component can be used to facilitate the consume of generic
APIs using HTTP. This library is able to configure the header, method, body, and
query string of a HTTP request according to specific API parameters.
This mapping is provided using a special PHP configuration array.
You can specify the API parameters using the setApi method. This method
accepts two parameters: the name of the API and a closure (callback) that
returns the configuration with a PHP array.
Let see an example, image you need to consume an authentication API call with a POST HTTP request using a JSON data format with the following parameters: username and password.
The HTTP request can be represented as follow:
POST /v1/auth HTTP/1.1
Host: localhost
Connection: close
Content-Type: application/json
Content-Length: 57
{ 'auth' : { 'username' : 'admin', 'password' : 'test' }}
You need to configure the API call using the setApi method in this way
(we use the auth name for this API):
use ZendService\Api\Api; $api = new Api(); $api->setApi('auth', function ($params) { return array( 'url' => 'http://localhost/v1/auth', 'header' => array( 'Content-Type' => 'application/json' ), 'method' => 'POST', 'body' => json_encode(array( 'auth' => array( 'username' => $params[0], 'password' => $params[1] ) )), 'response' => array( 'valid_codes' => array('200') ) ); });
After that you can execute the API call using the function auth (this function
is managed by the magic __call
function of PHP):
$result = $api->auth('username', 'password'); if ($api->isSuccess()) { var_dump($result); } else { printf("Error (%d): %s\n", $api->getStatusCode(), $api->getErrorMsg()); }
The mapping with the auth arguments and the API specification is managed using
the array $params. You have to use the numerical index of the $params to
match the order of the arguments in the function. Using the configuration array
you can specify all the HTTP data for the API request (headers, body, uri, etc).
You can also specify the HTTP status code for the successful requests using the
valid_codes parameter in the response section.
Using a configuration file
You can also use a configuration file for the API calls instead of using the
setApi method. You need to create a PHP file with the same name of the API
call. This file contains the API configuration array.
For instance, for the previous example you have to create a auth.php file
containing the following array:
return array( 'url' => 'http://localhost/v1/auth', 'header' => array( 'Content-Type' => 'application/json' ), 'method' => 'POST', 'body' => json_encode(array( 'auth' => array( 'username' => $params[0], 'password' => $params[1] ) )), 'response' => array( 'valid_codes' => array('200') ) );
You need to set the directory containing this configuration file using the
setApiPath as follow:
use ZendService\Api\Api; $api = new Api(); $api->setApiPath('path/to/api/config'); $result = $api->auth('username', 'password'); if ($api->isSuccess()) { var_dump($result); } else { printf("Error (%d): %s\n", $api->getStatusCode(), $api->getErrorMsg()); }
Set the base URL for the API calls
If you need to call different API from the same base URL you can use the
setUri function. This function set the base URL and you can use relative URI
for the specific API calls, for instance imagine you need to consume an OpenStack
service with the URL http://identity.api.openstack.org, we can set this address
as base URL and use relative address for each API call.
use ZendService\Api\Api; $api = new Api(); $api->setUrl('http://identity.api.openstack.org'); $api->setApi('authentication', function ($params) { return array( 'url' => '/v2.0/tokens', 'header' => array( 'Content-Type' => 'application/json' ), 'method' => 'POST', 'body' => json_encode(array( 'auth' => array( 'passwordCredentials' => array( 'username' => $params[0], 'password' => $params[1] ) ) )), 'response' => array( 'valid_codes' => array('200', '203') ) ); }); $result = $api->authentication('username', 'password'); if ($api->isSuccess()) { printf("Authenticate!\n"); } else { printf("Error (%d): %s\n", $api->getStatusCode(), $api->getErrorMsg()); }
Note the use of the relative address in the uri parameter of the API
configuration.
Query string in the API calls
If you need to pass a query string for an API HTTP call you can use the
setQueryParams method of the Api class. For instance, imagine you need to
pass the HTTP query string ?auth=strong in the previous example, you can use
the following code:
use ZendService\Api\Api; $api = new Api(); $api->setQueryParams(array( 'auth' => 'strong' )); $result = $api->authenticate('username', 'password'); if ($api->isSuccess()) { printf("OK!\n"); } else { printf("Error (%d): %s\n", $api->getStatusCode(), $api->getErrorMsg()); }
You can reset the query string calling the setQueryParams() function without a
parameter.
Set the default HTTP headers
You can specify a default HTTP headers to be used for all the HTTP calls. For instance, if you need to call a vendor API passing an authentication token using a special header field you can use this feature to set a default headers to be used for all the next API calls.
To set a default headers you can use the setHeaders function, below is
reported an example:
use ZendService\Api\Api; $api = new Api(); $api->setApiPath('path/to/api/config'); $api->setHeaders(array( 'X-Auth-Token' => 'token' )); $result = $api->test('foo'); if ($api->isSuccess()) { var_dump($result); } else { printf("Error (%d): %s\n", $api->getStatusCode(), $api->getErrorMsg()); }
The test API will execute a HTTP request using the headers specified in the
test.php configuration file plus the X-Auth-Token header. Basically, the
headers specified in the configuration file are merged with the default one
specified using the setHeaders function. You can overwrite the default headers
using the same header key in the configuration file.
zendframework/zendservice-api 适用场景与选型建议
zendframework/zendservice-api 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 40.34k 次下载、GitHub Stars 达 35, 最近一次更新时间为 2013 年 04 月 29 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「api」 「zend」 「Zend Framework」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 zendframework/zendservice-api 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 zendframework/zendservice-api 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 zendframework/zendservice-api 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
PHP Framework HLEB2 is the foundation of the web application. Provides ease of development and application performance.
Polyfill for mb_ereg(), mb_eregi(), mb_ereg_match(), and mb_ereg_replace*() functions; primary use case is for mbstring on Windows 7.4+
A PSR-7 compatible library for making CRUD API endpoints
Zend Framework 1 Http package
Zend Framework 1 Soap package
Simple asset loading
统计信息
- 总下载量: 40.34k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 35
- 点击次数: 21
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2013-04-29