neon-php/simple-http

为 Guzzle 提供简单的 Http 接口

v0.9.10 2022-02-21 14:29 UTC

This package is auto-updated.

Last update: 2024-09-21 20:23:40 UTC


README

安装

composer require neon-php/simple-http

简介

simple-http 提供了一个小型接口,使用 Guzzle-HTTP-Client 创建非常简单的 HTTP 请求。

用法

创建请求

创建新请求的最简单方法是通过使用静态外观类 Neon\Http\Facade\Http。此类提供静态方法来创建 Neon\Http\Http 类的新请求实例。

最简单的方式是通过调用对应 HTTP 动词的方法来创建请求

<?php

use Neon\Http\Facade\Http;

$response = Http::get('http://example.com/api');

支持以下方法和它们对应的 HTTP 动词:

<?php

use Neon\Http\Facade\Http;

$response = Http::get('http://example.com/api');
$response = Http::post('http://example.com/api');
$response = Http::put('http://example.com/api');
$response = Http::patch('http://example.com/api');
$response = Http::delete('http://example.com/api');

设置基本 URL

通过使用方法 setBaseURL,您可以设置一个基本 URL,该 URL 将用于所有后续请求。

<?php

use Neon\Http\Facade\Http;

Http::setBaseURL('http://example.com');

$response = Http::get('/api');

框架请求方法

某些框架可能需要一个特殊的请求参数来发送以确定请求方法。例如,有时一个 put 请求基本上是一个包含以下请求参数的 post 请求。

_method: put

为了缩短此过程,可以通过使用框架方法激活相应的功能,可以使用方法 setFrameworkMethod

<?php

use Neon\Http\Facade\Http;

Http::setBaseURL('http://example.com');
Http::setFrameworkMethod(true);

$response = Http::put('/api');

请求输入

要将输入值添加到您的请求(POST、PUT、PATCH、DELETE)中,您可以在调用请求方法之前调用 addParam 方法提供键/值对。

<?php

use Neon\Http\Facade\Http;

$response = Http::addParam('key', 'value')->post('/api');

或者,您可以将一个数组作为请求方法的第二个参数添加,以提供多个键/值对。

<?php

use Neon\Http\Facade\Http;

$response = Http::post('/api', [
    'some_key' => 'some_value',
    'another_key' => 'another_value'
]);

要为 GET 请求提供查询参数,您需要向输入数组提供 query-键。

<?php

use Neon\Http\Facade\Http;

$response = Http::post('/api', [
    'query' => [
        'some_key' => 'some_value',
        'another_key' => 'another_value'
    ]
]);

// Resulting request url:
// http://example.com/api?some_key=some_value&another_key=another_value

添加请求头

要添加额外的请求头,您可以在调用请求方法之前调用 addHeader 方法。

<?php

use Neon\Http\Facade\Http;

$response = Http::addHeader('Accept', 'application/json')->post('/api');

要快速将 Bearer 令牌添加到授权头中,您可以使用 bearer 方法。

<?php

use Neon\Http\Facade\Http;

$response = Http::bearer($token)->post('/api');

添加文件

可以通过 file 方法添加文件。只需提供键、文件名和文件位置即可。

<?php

use Neon\Http\Facade\Http;

$image = $_FILES['image'];

$response = Http::file('image', $image['name', $image['tmp_name']])->post('/api');

响应处理

在创建请求后,相应的方法将返回一个 Neon\Http\Response 实例。该对象提供了一些方法来处理响应。

头信息

方法 hasHeader 检查提供的头键是否存在于响应中。

<?php

use Neon\Http\Facade\Http;

$response = Http::get('http://example.com/api');

if ($response->hasHeader('Content-Length')) {
    // Header Content-Length exists
}

方法 getHeader 返回给定头键的头值。值作为字符串数组返回。

<?php

$values = $response->getHeader('Content-Length');

状态码

方法 code 返回响应的 HTTP 状态码。

<?php

$code = $response->code();

此外,还有几个方法可以检查特定的预定义状态码。

<?php

// Status code is 200, 201 or 204
$response->successfull();

// Status code is bigger than or equal to 400
$response->failure();

// Status code is bigger than or equal to 500
$response->serverError();

// Status code is bigger than or equal to 400 AND less than 500
$response->clientError();

// Status code is 201
$response->created();

// Status code is 204
$response->noContent();

// Status code is 404
$response->notFound();

// Status code is 401
$response->unauthorized();

// Status code is 403
$response->forbidden();

// Status code is 400
$response->badRequest();

响应体

方法 body 返回响应体作为字符串。

<?php

$body = $response->body();

方法 bodyRaw 返回响应体作为 Psr\Http\Message\StreamInterface 实例。

<?php

$body_stream = $response->bodyRaw();

方法 json 将体字符串转换为 PHP 数组并返回它。如果体不是有效的 JSON 字符串,则该方法抛出 Neon\Http\Exceptions\RequestException

<?php

use Neon\Http\Exceptions\RequestException;

try {
    $body = $response->json();
} catch (RequestException $e) {
    // Response body is not json
}

错误处理

如果请求由于任何原因不成功,方法 getpostputpatchdelete 都会抛出 Neon\Http\Exceptions\RequestException