kwhat/requestful

一个简单的PSR兼容的HTTP客户端库

v0.2.1 2022-06-04 18:24 UTC

This package is auto-updated.

Last update: 2024-09-04 23:21:12 UTC


README

Latest Stable Version Latest Unstable Version Build Status Total Downloads License composer.lock available

Requestful:一个简单的PSR兼容的HTTP客户端库

该库的目标是提供一个简洁、简单且易于理解的替代方案,以Guzzle

  • 使用最新的PSR标准和推荐
  • 支持同步和异步HTTP请求
  • A+ Promises 与Guzzle兼容的接口
  • 支持现代PHP 7
  • 严格要求ext-curl扩展

安装

composer require kwhat/requestful

用法

您需要一个PSR-17实现来使用此库。我个人喜欢使用nyholm/psr7,但任何提供psr/http-factory的库都足够。

$factory = new Nyholm\Psr7\Factory\Psr17Factory();
$client = new Requestful\Http\Client($factory);
$request = $factory->createRequest(
    "GET", 
    "https://samples.openweathermap.org/data/2.5/weather?q=Los%20Angeles"
);

// Send a synchronous request
$response = $client->sendRequest($request);

var_dump(
    $response->getStatusCode(), // int(200)
    $response->getHeaderLine("content-type"), // string(30) "application/json; charset=utf8"
    $response->getBody() // string(459) "{"coord": {...}, "weather": {...}, ...}"
);

// Send an asynchronous request
$promise = $client->sendRequestAsync($request)
    ->then(function (Psr\Http\Message\ResponseInterface $response): string {
        return "Success: {$response->getStatusCode()}";
    });

var_dump($promise->wait()); // string(12) "Success: 200"