slexphp/simple-http-api-client

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

v3.0.0 2021-10-29 06:47 UTC

This package is auto-updated.

Last update: 2024-08-29 05:13:16 UTC


README

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

安装

通过在项目目录中运行 composer require slexphp/simple-http-api-client 使用 Composer 安装。

公共 API

Slexphp\Http\SimpleApiClient\Psr\JsonClientFactory\JsonApiClient

此类提供静态 create() 方法。

它接受一个 psr 客户端和请求工厂,并创建一个 ApiClientInterface 实例(见下文)。

create() 方法的第一个两个参数(以及返回类型和抛出异常的接口),是该库公共 API 的唯一部分。

实际上返回的类以及其背后的任何细节在此阶段不应被依赖。

$jsonClient = JsonApiClient::create($psrRequestFactory, $psrClient);

接口

Slexphp\Http\SimpleApiClient\Contracts\ApiClientInterface

这是所有 API 客户端交互的主接口。它提供了一个方法,包含发送 HTTP 请求所需的所有参数。

$client->call($baseUrl, $method, $endpoint, $query, $headers, $body);

它还将根据传递和接收的内容类型头处理请求体序列化和响应体反序列化。

实现应默认回退到 application/json 内容类型请求体序列化器,如果没有提供请求内容类型头。实现也应默认回退到 application/json 内容类型响应体反序列化器,如果没有从服务器接收响应内容类型头。

尽管如此,建议在请求头中显式传递请求内容类型。因为使用默认请求体序列化器并不表示向实际请求添加默认内容类型头。

Slexphp\Http\SimpleApiClient\Contracts\ApiResponseInterface

此接口描述 API 响应对象。它与 PSR-7 \Psr\Http\Message\ResponseInterface 类似,但没有修改器方法,并提供了 getParsedBody() 方法,因此您无需担心解析。

Slexphp\Http\SimpleApiClient\Contracts\ApiClientExceptionInterface

此接口描述在 HTTP 调用期间或请求/响应消息的(反)序列化期间可能发生的所有可能的错误。

示例

在这个例子中,我们已经创建了一个客户端实例,并执行了一个 HTTP 调用。然后我们展示了响应/错误 API 的所有方法,同时执行了所有始终为真的断言。

try {
    $response = $jsonClient->call(
        'https://myapi.com',
        'POST',
        '/some/endpoint',
        ['queryParam' => 'value'], // query
        ['Authorization' => 'Basic u:p'], // headers
        ['bodyProperty' => 'value'] // body
    );
} catch (ApiClientExceptionInterface $e) {
    $code = $e->getCode();
    var_dump($code);
    
    $response = $e->getResponse();
    if (!$response) {
        // connect error
        assert($code === 0);
        throw $e;
    }
    
    $status = $response->getStatusCode();
    var_dump($status);
    assert($code === $status);
    var_dump($response->getHeaders());
    
    if ($response->getRawBody()) {
        var_dump($response->getRawBody());
        assert($response->getRawBody() !== '');
        if ($response->getParsedBody() === null) {
            // response body parse error, any http status code
        } else {
            // non 2xx response
            assert($status < 200 || $status >= 300);
            var_dump($response->getParsedBody());
            assert(\is_array($response->getParsedBody());
        }
    } else {
        // no response body, only non 2xx responses
        assert($status < 200 || $status >= 300);
        assert($response->getRawBody() === '');
        assert($response->getParsedBody() === null);        
    }
    
    throw $e;
}

// 2xx responses succesfully parsed

$status = $response->getStatusCode();
var_dump($status);
assert($status >= 200 && $status < 300);
var_dump($response->getHeaders());

if ($response->getRawBody()) {
    var_dump($response->getRawBody());
    assert($response->getRawBody() !== '');
    var_dump($response->getParsedBody());
    assert(\is_array($response->getParsedBody());
} else {
    assert($response->getRawBody() === '');
    assert($response->getParsedBody() === null);        
}

ApiClientInterface 实现是无状态服务(只要底层响应工厂和客户端都是无状态的)。您可以只使用一个实例进行所有后端到后端的调用。