decodelabs/hydro

基于 Guzzle 的简单 PSR18 HTTP 客户端包装器

v0.1.5 2024-08-22 01:33 UTC

This package is auto-updated.

Last update: 2024-09-04 21:17:42 UTC


README

PHP from Packagist Latest Version Total Downloads GitHub Workflow Status PHPStan License

基于 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