nimbly/shuttle
Composer 安装命令:
composer require nimbly/shuttle
包简介
Simple PSR-18 HTTP client.
README 文档
README
A simple PSR-18 HTTP client library.
Requirements
- PHP 8.2+
- ext-curl
Installation
composer require nimbly/shuttle
Features
- Responses create php://temp response body stream and swap to disk when necessary.
- cURL (default) and Stream Context handlers supported.
- Middleware support out of the box.
- Easy body transformations when creating requests with JsonBody and FormBody helper classes.
- Support for multipart form bodies.
Not features
- Asynchronous calls.
A note on PSR-7 and PSR-17
Shuttle makes use of PSR-7 HTTP Message and will default to using nimbly/capsule. You can override this by providing your perferred choice in PSR-7 implementations by passing PSR-17 HTTP Factories instances into the constructor of Shuttle.
$http_factory = new GuzzleHttp\Psr7\HttpFactory; $shuttle = new Shuttle( requestFactory: $http_factory, responseFactory: $http_factory, streamFactory: $http_factory, uriFactory: $http_factory, );
Making requests: The easy way
The quickest and easiest way to begin making requests in Shuttle is to use the HTTP method name:
use Nimbly\Shuttle\Shuttle; $shuttle = new Shuttle; $response = $shuttle->get("https://www.google.com"); $response = $shuttle->post("https://example.com/search", "Form data"));
Shuttle has built-in methods to support the major HTTP verbs: get, post, put, patch, delete, head, and options. However, you can make any HTTP verb request using the request method directly.
$response = $shuttle->request("connect", "https://api.example.com/v1/books");
Handling responses
Responses in Shuttle implement PSR-7 ResponseInterface and as such are streamable resources.
$response = $shuttle->get("https://api.example.com/v1/books"); echo $response->getStatusCode(); // 200 echo $response->getReasonPhrase(); // OK $body = $response->getBody()->getContents(); // {"title": "Do Androids Dream of Electric Sheep?", "author": "Philip K. Dick"}
Handling failed requests
Shuttle will throw a RequestException by default if the request failed. This includes things like host name not found, connection timeouts, etc.
Responses with HTTP 4xx or 5xx status codes will not throw an exception and must be handled properly within your business logic.
Making requests: The PSR-7 way
If code reusability and portability is your thing, future proof your code by making requests the PSR-7 way. Remember, PSR-7 stipulates that Request and Response messages be immutable.
// Build Request message with your favorite PSR-7 library. $request = new Request("get", "https://www.example.com"); // Send the Request. $shuttle = new Shuttle; $response = $shuttle->sendRequest($request);
Using the sendRequest() method does not apply any base_url or default headers passed into the Shuttle constructor. However, the request is still passed through the middleware chain.
Request bodies
An easy way to submit data with your request is to use the Nimbly\Shuttle\Body\* helper classes. These classes will automatically transform the data, convert to a BufferStream, and set a default Content-Type header on the request.
The request bodies supported are:
JsonBodyConverts an associative array or instance ofJsonSerializableinto JSON and sets theContent-Typeheader toapplication/json.FormBodyConverts an associative array into a query string, setsContent-Typeheader toapplication/x-www-form-urlencoded.
To submit a JSON payload with a request:
use Nimbly\Shuttle\Body\JsonBody; $book = [ "title" => "Breakfast Of Champions", "author" => "Kurt Vonnegut", ]; $shuttle->post("https://api.example.com/v1/books", new JsonBody($book));
Middleware
Shuttle supports dual (aka double) pass middleware by implementing MiddlewareInterface. The request and response instance are both available to the middleware and can be manipulated to your specific needs.
class AuthMiddleware implements MiddlewareInterface { public function __construct( private string $api_key) { } public function process(RequestInterface $request, callable $next): ResponseInterface { // Add the Authorization header with every outgoing request. $request = $request->withAddedHeader("Authorization", "Bearer " . $this->api_key); // Pass request object to next middleware layer. $response = $next($request); // Return response back with custom header added. return $response->withAddedHeader("X-Custom-Header", "Foo"); } }
You may add as many middleware layers as you need and pass them to the Shuttle constructor. The middleware are executed in the order given.
$shuttle = new Shuttle( middleware: [ new AuthMiddleware(\getenv("API_KEY")), new FooMiddleware, new BazMiddleware, ] );
nimbly/shuttle 适用场景与选型建议
nimbly/shuttle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4.24M 次下载、GitHub Stars 达 5, 最近一次更新时间为 2019 年 02 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「curl」 「http」 「http client」 「client」 「stream context」 「psr-18」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 nimbly/shuttle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 nimbly/shuttle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 nimbly/shuttle 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
cURL and cURL based Soap-Client for PHP
repository php library
Simple cURL wrapper
Mock PSR-18 HTTP client
A fluent PHP CURL wrapper
Asynchronous MQTT client built on React
统计信息
- 总下载量: 4.24M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 25
- 依赖项目数: 17
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-02-13