technicalguru/rest-client 问题修复 & 功能扩展

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

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

technicalguru/rest-client

Composer 安装命令:

composer require technicalguru/rest-client

包简介

A PHP library for accessing REST services

README 文档

README

A lightweight REST client based on php-curl.

License

This project is licensed under GNU LGPL 3.0.

Installation

By Composer

composer install technicalguru/rest-client

By Package Download

You can download the source code packages from GitHub Release Page

How to use

Creating a Request

Depending on your intent, all you need is the URL and the body if required:

use TgRestClient\Request;

$request = Request::get('https://www.example.com/endpoint');
$request = Request::put('https://www.example.com/endpoint', $myObject);
$request = Request::post('https://www.example.com/endpoint', $myObject);
$request = Request::delete('https://www.example.com/endpoint/123');

Request provides a static method for each of the HTTP methods: HEAD, GET, POST, PUT, PATCH, OPTIONS, DELETE.

Setting Request Headers

Additional headers for the request can be set like this:

use TgRestClient\Headers;

// individual headers
$request
    ->setHeader(Headers::AUTHORIZATION, 'Bearer 1234567890')
    ->setHeader(Headers::COOKIE,        'XSESSION=qwefjnqlkiu117o11ndn1');
    
// set multiple headers with a (key => value) array
$request->addHeaders($headers);

The Request Body

By default, Request assumes your body needs to be of application\json type. As such, the implementation already knows how to build a JSON encoding and you can pass an object or array for your body. Request will automatically stringify. Same is valid for application/x-www-form-urlencoded content. However, you will need to set the Content-Type header accordingly:

use TgRestClient\Request;
use TgRestClient\Headers;

$parameters = array(
    'name1' => 'value1',
    'name2' => 'value2',
);
$request = Request::post($url, $parameters)
    ->setHeader(Headers::CONTENT_TYPE, Headers::TYPE_X_WWW_FORM_URLENCODED);

Executing

Once you configured your request, you can simply execute it:

$response = $request->execute();

The execute call takes an optional argument - the number of seconds until the call times out. The default timeout is 5 seconds. The method will return latest after this time and give you a Response object.

The Response

The response object returns everything you need to know about the result of your call:

$httpCode    = $response->getHttpCode();
$bodyObject  = $response->getBody();
$rawBody     = $response->getRawBody();
$headerValue = $response->getHeader('Server');

The getBody() method returns objects when the server returned a JSON-encoded body. Otherwise, you will get the raw body by this method too.

Defining Default Request Headers

It can be very exhausting to set the same request headers for each individual request over and over again. That's why you can define default headers:

use TgRestClient\Headers;

// Single header
Headers::setDefaultHeader('www.example.com', Headers::AUTHORIZATION, 'Bearer 1234567890');

// or multiple
Headers::addDefaultHeaders('www.example.com', $myDefaultHeaders);

The headers will be available for each request to www.example.com. However, you can override headers for individual requests if required. Just set the header on the request object.

Executing Requests Simultaneously

One strength of the library is the ability to perform multiple requests at the same time:

use TgRestClient\Client;
use TgRestClient\Request;

// Build your request objects
$request1  = Request::get($url1);
$request2  = Request::get($url2);
...

// Create the client
$client = new Client();

// Add the requests to the client and get the Response objects
$response1 = $client->addCall($request1);
$response2 = $client->addCall($request2);
...

// Now execute all together
$responses = $client->run();

Please notice that the Response objects cannot be used before you called the run() method on the client.

The run() method again can take a timeout in seconds (which defaults to 5). It also returns an array of the Response objects in the order of how you added them to the client. That means, the first response belongs to the very first request you added, the seconds response to the second request you added, and so on.

You can also ask the Response object which Request it was executing:

$request1 = $responses[0]->getRequest();
$request2 = $responses[1]->getRequest();

Developer Remark

The PHP Unit test will require a gorest.co.in API token. Most tests will not be executed without this token stored in environment variable GOREST_TOKEN.

Contribution

Report a bug, request an enhancement or pull request at the GitHub Issue Tracker.

technicalguru/rest-client 适用场景与选型建议

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

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: LGPL-3.0-or-later
  • 更新时间: 2020-11-15