承接 adiafora/php-api-client 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

adiafora/php-api-client

Composer 安装命令:

composer require adiafora/php-api-client

包简介

Packet to send to an Api using PHP.

README 文档

README

This package makes it very easy to access any API using both GET and POST requests. You can also send headers and parameters.

Installation

Run:

    composer require "adiafora/php-api-client"

Configurations

You can set parameters for curl using global constants.

SET_CURLOPT_CONNECTTIMEOUT - The number of seconds to wait while trying to connect. Use 0 to wait indefinitely. Default is 10. SET_CURLOPT_TIMEOUT - The maximum number of seconds to allow cURL functions to execute. Default is 10.

Usage

You must create a class that extends the Adiafora\ApiClient\AbstractApiClient abstract class. And define the methods:

getHeaders() - returns an array of headers.

getUrl() - returns a string, the API url.

And in your project, you must refer to this class, which extends the Adiafora\ApiClient\AbstractApiClient.

When accessing your class, you can pass the GET or POST request type (by default GET) and an array of request parameters, if any, to the constructor method. if you execute a GET request without parameters, you don't need to pass anything to the constructor method.

 //Example

 $api = new MyApiClient('POST', ['name' => 'Sergey']);
 // or
 $api = new MyApiClient();
 

To make a request to the API, call the send() method:

 $response = $api->send();

The method returns an object of class Adiafora\ApiClient\ApiResponse that contains these methods:

code() - return code of response.

response() - return response from the API decoded using json_decode(). Because most APIs return a response in json format. If you want to get the answer as it is, then use the dirty() method.

dirty() - return the answer as it is without transformations.

error() - return curl_error() for the curl.

errno() - return curl_errno() for the curl.

requestParameters() - return parameters of request.

requestUrl() - return url of request.

requestHeaders() - return headers of request.

totalTime() - return total transaction time in seconds for last transfer.

Real example

For example, we need to connect to the Yandex.Webmaster API.

For more information, see the API documentation https://yandex.com/dev/webmaster/doc/dg/concepts/About.html/

We are creating a class WebmasterApiClient

    use Adiafora\ApiClient\AbstractApiClient;
    
    class WebmasterApiClient extends AbstractApiClient
    {
        private $resource;    

        private $token;
    
        private $userId;
    
        public function __construct(string $resource, string $method, array $params = [])
        {
            parent::__construct($method, $params);
    
            $this->resource = $resource;
            $this->userId   = USER_ID;
            $this->token    = TOKEN;
        }
    
        public function getHeaders(): array
        {
            return [
                'Authorization: OAuth ' . $this->token,
                'Accept-Language: ru',
                'Content-Type: application/json; charset=utf-8'
            ];
        }
    
        public function getUrl(): string
        {
            return 'https://api.webmaster.yandex.net/v4/user/' . $this->userId . '/' . $this->resource;
        }
    }

We have implemented 2 required methods getHeaders() and getUrl(). Everything else, including the implementation of the constructor method, is only necessary for implementing a specific API.

Now we want to get a list of all hosts. There is nothing easier! Just:

// @see https://yandex.com/dev/webmaster/doc/dg/reference/hosts-docpage/
$api = new WebmasterApiClient('hosts', Adiafora\ApiClient\Method::GET);
$response = $api->send();

$response->code(); // 200
$response->response(); // array()

Or getting the history of SQI changes using the parameters:

// @see https://yandex.com/dev/webmaster/doc/dg/reference/sqi-history.html/
$api = new WebmasterApiClient('hosts/' . $hostId . '/sqi-history', Adiafora\ApiClient\Method::GET, [
            'date_from' => '2016-01-01T00:00:00,000+0300',
        ]);
$api->send(); 

License

The MIT License (MIT). Please see License File for more information.

adiafora/php-api-client 适用场景与选型建议

adiafora/php-api-client 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 99 次下载、GitHub Stars 达 1, 最近一次更新时间为 2020 年 10 月 18 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「php」 「http client」 「api」 「api client」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 adiafora/php-api-client 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 adiafora/php-api-client 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-10-18