thomas-squall/php-easy-api
Composer 安装命令:
composer require thomas-squall/php-easy-api
包简介
PHP API System for client and server development.
README 文档
README
Easy to use library which takes advantage of the PHP7 annotations library.
Installation
Using composer is quite simple, just run the following command:
$ composer require thomas-squall/php-easy-api
Before starting
Please remember that in order to work you should make sure that every endpoint you call the resolver snippet should be called too. I've attached an htaccess-example that could help. If you got any doubt please open an issue here.
Usage Example
Client
Let's assume we've got the following class:
<?php /** * Class Mailchimp. * [\PHPEasyAPI\Client] */ class Mailchimp { /** * [\PHPEasyAPI\Enrichment\Endpoint(method = "GET", url = "https://{$dataCenter}.api.mailchimp.com/3.0/lists/")] * [\PHPEasyAPI\Enrichment\User(username = "{$username}", password = "{$apiKey}")] */ public $getLists; public $dataCenter; public $username; public $apiKey; }
This is a basic implementation to call the lists/ mailchimp endpoint.
PS: Note that {$dataCenter}, {$username} and {$password} special strings are used.
This special strings will evaluate with the class corresponding field value.
Now if we want to proceed and make the call we just need to do:
$mailchimp = new Mailchimp(); $mailchimp->dataCenter = '<YourDatacenter>'; $mailchimp->username = '<YourUsername>'; $mailchimp->apiKey = '<YourAPIKey>'; $resolver = new \PHPEasyAPI\Resolver(); $resolver->makeRequest($mailchimp, 'getLists'); print_r($mailchimp->getLists);
Let's analyze:
- We instantiated a Mailchimp object.
- We filled the needed values for the call.
- We instantiated a \PHPEasyAPI\Resolver object.
- We called the
makeRequestmethod of the resolver passing the Mailchimp object (which is a API Client as per annotations) and the name of the field containing the Endpoint annotation. - We printed the value of the field containing the endpoint of the annotation.
PS: Note that the result will be put inside the field itself.
Server
Let's assume we've got the following class:
<?php /** * Class Listener. * [\PHPEasyAPI\Server("user")] // 'user' is the endpoint */ class Listener { /** * @param \PHPEasyAPI\Request $request * @return string * [\PHPEasyAPI\Enrichment\Endpoint(method = "GET", url = ":userId/getList/:listId")] */ public function getList($request) { $userId = $request["userId"]; $listId = $request["listId"]; $request->send200("List $listId of user $userId"); } }
And we want to listen for incoming calls at the getList method of the user.
To do that we need to bind the listener to an endpoint in that way:
$resolver->setBaseUrl('http://localhost/MyTest'); // Assuming this is your local test url. $resolver->bindListener(new Listener()); // 'user' is the endpoint.
NB: The base url is needed to make the resolver understand which part of the request url do not compute. Not setting it will throw an Exception as it is crucial for the system to work.
Now what remains to do is to resolve incoming requests:
$resolver->resolve(); // Example call: GET http://localhost/MyTest/user/10/getList/15
Let's analyze:
- We created a
Listenerclass. - We annotated the class with the
\PHPEasyAPI\Serverannotation. - We created a method to handle calls and annotated with the
\PHPEasyAPI\Enrichment\Endpointannotation. - In the Endpoint annotation we passed the method (
GETin this case) we wanted to handle and the url structure (Note that:userIdans:listIdare placeholder and they will be substituted with the corresponding part of the url and passed to the function as parameters on the given order). - We bound the
'user'endpoint to an instance of theListener. - We resolved the requested url.
thomas-squall/php-easy-api 适用场景与选型建议
thomas-squall/php-easy-api 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 100 次下载、GitHub Stars 达 3, 最近一次更新时间为 2018 年 01 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 thomas-squall/php-easy-api 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 thomas-squall/php-easy-api 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 100
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 5
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-01-15