palanik/wrapi
Composer 安装命令:
composer require palanik/wrapi
包简介
Wrapper for calling Restful API
README 文档
README
Wrapper for calling Restful API
wrapi allows you to call HTTP based APIs just like making calls to ordinary php functions.
Installation
Install with Composer
- Update your
composer.jsonto requirepalanik/wrapipackage. - Run
composer installto add wrapi your vendor folder.
{
"require": {
"palanik/wrapi": "*"
}
}
or simply run
composer require palanik/wrapi
Easy Start
Approach I
- Create an array listing all the API endpoints you want to work with.
- Wrap endpoints with
wrapi. - Call individual endpoints as functions.
See Example Code
Approach II
- Create client object with API Base URL.
- Register API endpoints.
- Call individual endpoints as functions.
See Example Code
Endpoints Array
Declare each endpoint as per the following specifications.
"function_name" => array( "method" => "HTTP_METHOD", // 'GET', 'POST', 'PUT', 'PATCH' or 'DELETE' "path" => "relative/path/to/:api/endpoint" // Can use `Slim`/`express` style path params )
eg. a small set of github.com API
array( "repo" => array( "method" => "GET", "path" => "repos/:owner/:repo" ), "contributors" => array( "method" => "GET", "path" => "repos/:owner/:repo/contributors" ), "languages" => array( "method" => "GET", "path" => "repos/:owner/:repo/languages" ), "tags" => array( "method" => "GET", "path" => "repos/:owner/:repo/tags" ), "branches" => array( "method" => "GET", "path" => "repos/:owner/:repo/branches" ) )
Wrap endpoints
Create a API client object from wrapi. Provide the base url for the API and the endpoints array.
wrapi will create a client object with all the necessary functions.
$endpoints = array(...); $client = new wrapi\wrapi('https://api.github.com/', // base url for the API endpoints // your endpoints array ); // client object contains functions to call the API
Register
Register additional API endpoints with the client object with a function name.
$client("zen", array( "method" => "GET", "path" => "zen" ) );
Make the call
Call the functions with arguments.
// This will make GET request to 'https://api.github.com/repos/guzzle/guzzle/contributors' $contributors = $client->contributors('guzzle', 'guzzle'); $zenQuote = $client->zen(); echo "Today's quote: ". $zenQuote;
API
wrapi is an open ended framework and is not restricted any one or a set of public APIs. All APIs providing HTTP interface to access the endpoints can be wrapped by wrapi so that you can quickly build your client application.
Endpoint definition
method & path/url are required.
method- Any one of the HTTP methodspath- route path to API Endpoint. Supportsexpressstyle path paramsquery- an associative array with name-value pairs. This is optional. Useful where resources are identified via query string parametersurl- fully qualified uri string to override. Useful when api calls connect to a different endpoints
Client object
The wrapi object conveniently provides the client interface to the API. Create it by calling new wrapi\wrapi().
The constructor takes the following arguments:
baseURL- The base url for the API. eg.https://api.github.com/repos/guzzle/guzzle/contributorsendpoints- The array listing the endpoints of the API. Provide an empty array or a partial list and register endpoints later.options- Optional parameter.wrapiuses Guzzle module to connect to API server. Theoptionsparameter is the sameoptionsparameter used inGuzzle``request.
Register function
Add endpoints to client object.
$client(function_name, endpoint_definition)
function_name- Alias for the endpoint, also the name of the function to call.endpoint_definition- Array defining the endpoint.
Function calls
Call the API via the function in the client object. Arguments to the function depend on the API declaration in the endpoints array.
Provide the arguments in the following order:
- named
paramsin the url path of the endpoint. eg.$client->contributors('guzzle', 'guzzle') // guzzle (owner) & guzzle (repo) are path params querystringas an associative array with name-value pairs. eg.$client->contributors(array("since" => 364) // querystring ?since=364body- JSON content forPOSTorPUTmethods. Skip this argument if not required.
Examples
In examples folder.
License
palanik/wrapi 适用场景与选型建议
palanik/wrapi 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 323.11k 次下载、GitHub Stars 达 4, 最近一次更新时间为 2015 年 11 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「api」 「restful」 「Guzzle」 「function」 「wrapper」 「functions」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 palanik/wrapi 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 palanik/wrapi 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 palanik/wrapi 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A PSR-7 compatible library for making CRUD API endpoints
This is a restful api to onesignal.
Cloudflare Middleware For Guzzle
A RESTful API package for the Laravel and Lumen frameworks.
Simplifies working with holaluz API endpoints
Wrapper for Slack Web API
统计信息
- 总下载量: 323.11k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 23
- 依赖项目数: 4
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-11-02