decodelabs / hydro
基于 Guzzle 的简单 PSR18 HTTP 客户端包装器
v0.1.5
2024-08-22 01:33 UTC
Requires
- php: ^8.1
- decodelabs/atlas: ^0.12
- decodelabs/coercion: ^0.2.7
- decodelabs/exceptional: ^0.4.4
- decodelabs/veneer: ^0.11.6
- guzzlehttp/guzzle: ^7.0
- psr/http-client: ^1.0
- psr/http-message: ^2.0
Requires (Dev)
- decodelabs/collections: ^0.9
- decodelabs/phpstan-decodelabs: ^0.6.6
Suggests
- decodelabs/collections: Fetch JSON files over HTTP as Tree
README
基于 Guzzle 的简单 HTTP 客户端包装器
Hydro 使用 Guzzle 在底层提供简单接口,实现常见 HTTP 客户端功能。
关注 DecodeLabs 博客 获取最新信息和更新。
安装
通过 Composer 安装
composer require decodelabs/hydro
用法
不同类型文件的简单用法
use DecodeLabs\Hydro; $memoryFile = Hydro::get('https://example.com/file.txt'); // Atlas file $string = Hydro::getString('https://example.com/file.txt'); // String $file = Hydro::getFile('https://example.com/file.txt', '/path/to/save/file.txt'); // Local file $tempFile = Hydro::getTempFile('https://example.com/file.txt'); // Temp file $json = Hydro::getJson('https://example.com/file.json'); // Decoded JSON array $tree = Hydro::getJsonTree('https://example.com/file.json'); // Decoded JSON Collections/Tree
选项
向底层客户端传递一个包含选项(包括 URL)的数组
Hydro::get([ 'url' => 'https://example.com/file.txt', 'timeout' => 10 ]);
错误
处理错误状态响应(或返回替代响应)
$file = Hydro::get('https://example.com/file.txt', function($response) { switch($response->getStatusCode()) { case 404: throw Exceptional::Notfound('File not found'); case 500: throw Exceptional::Runtime('Server error'); default: return Hydro::request('GET', 'https://example.com/other.txt'); } });
许可协议
Hydro 采用专有许可协议。有关完整的许可协议文本,请参阅 LICENSE。