thomas-squall/php-easy-api 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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:

  1. We instantiated a Mailchimp object.
  2. We filled the needed values for the call.
  3. We instantiated a \PHPEasyAPI\Resolver object.
  4. We called the makeRequest method 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.
  5. 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:

  1. We created a Listener class.
  2. We annotated the class with the \PHPEasyAPI\Server annotation.
  3. We created a method to handle calls and annotated with the \PHPEasyAPI\Enrichment\Endpoint annotation.
  4. In the Endpoint annotation we passed the method (GET in this case) we wanted to handle and the url structure (Note that :userId ans :listId are placeholder and they will be substituted with the corresponding part of the url and passed to the function as parameters on the given order).
  5. We bound the 'user' endpoint to an instance of the Listener.
  6. 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 我们能提供哪些服务?
定制开发 / 二次开发

基于 thomas-squall/php-easy-api 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 100
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 5
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-01-15