innmind/http

值对象以抽象 http 消息

7.1.0 2024-06-27 09:27 UTC

README

Build Status codecov Type Coverage

不可变值对象和接口,用于抽象 http 消息。

重要:您必须使用 vimeo/psalm 来确保正确使用此库。

构建一个 ServerRequest

use Innmind\Http\Factory\ServerRequest\ServerRequestFactory;

$request = ServerRequestFactory::default()();

发送一个 Response

use Innmind\Http\{
    Response,
    Response\StatusCode,
    ProtocolVersion,
    Headers,
    Header,
    Header\ContentType,
    Header\ContentTypeValue,
    ResponseSender,
};
use Innmind\Filesystem\File\Content;
use Innmind\TimeContinuum\Earth\Clock;

$response = Response::of(
    StatusCode::ok,
    ProtocolVersion::v11,
    Headers::of(
        ContentType::of('application', 'json'),
    ),
    Content\Lines::ofContent('{"some": "data"}'),
);

(new ResponseSender(new Clock))($response);

将构建以下消息

HTTP/1.1 200 OK
Date: Wed, 04 May 2016 14:24:14 +0000
Content-Type : application/json

{"some": "data"}

构建一个多部分 Request

use Innmind\Http\{
    Request,
    Method,
    Content\Multipart,
    Header\ContentType,
    Header\ContentType\Boundary,
    Headers,
    ProtocolVersion,
};
use Innmind\Filesystem\{
    File\File,
    File\Content,
};
use Innmind\Url\Url;

$boundary = Boundary::uuid();
$request = Request::of(
    Url::of('http://some-server.com/')
    Method::post,
    ProtocolVersion::v11,
    Headers::of(ContentType::of('multipart', 'form-data', $boundary)),
    Multipart::boundary($boundary)
        ->with('some[key]', 'some value')
        ->withFile('some[file]', File::named(
            'whatever.txt',
            Content::ofString(' can be any file content'),
        )),
);