pipe / request
Blazingly fast php http request util
Requires
- php: >=8.2
Requires (Dev)
- pestphp/pest: ^3.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-12-02 13:46:59 UTC
README
Simple, pure PHP HTTP request reader
Installation
composer require pipe/request
Requires PHP 8.2 or newer. No runtime dependencies.
Usage
use Pipe\Request; $request = Request::fromGlobals(); // snapshot of $_GET, $_POST, $_SERVER, $_COOKIE, $_FILES $request->method(); // "POST" $request->path(); // "/folder/article-title,234.html" (no query string) $request->get('dir'); // query parameter, or null $request->post('name', 'anonymous'); // form field with a default $request->json('email'); // field of a JSON body $request->header('content-type'); // case-insensitive
For tests, CLI scripts and workers, build a request from arrays. Nothing is read from the superglobals:
$request = new Request( query: ['dir' => '2'], server: ['REQUEST_METHOD' => 'PUT', 'REQUEST_URI' => '/a?dir=2', 'CONTENT_TYPE' => 'application/json'], body: '{"name":"Ann"}', );
API
| Method | Description |
|---|---|
new Request(array $query = [], array $post = [], array $server = [], array $cookies = [], array $rawFiles = [], string|callable|null $body = null) |
Build from arrays. $body may be a callable, which is called at most once on first use. |
Request::fromGlobals(): static |
Snapshot of the current PHP request. The body is read from php://input lazily. |
Request::getInstance() / request() |
A shared fromGlobals() instance. |
method(): string |
Upper-cased method. "GET" when unknown. |
uri(): string |
The raw request target, including the query string. |
path(): string |
The path only: still percent-encoded, without the query string or fragment. "/" when empty. |
get(?string $key = null, mixed $default = null) / query(...) |
A query parameter, or every query parameter when called without a key. |
post(?string $key = null, mixed $default = null) |
A form field, or every field without a key. $_POST for POST. For PUT, PATCH and DELETE, the body is parsed when it is application/x-www-form-urlencoded. |
json(?string $key = null, mixed $default = null) |
A field of the JSON-decoded body (arrays are associative). An empty body decodes to []. Throws JsonException for malformed JSON. |
input(?string $key = null, mixed $default = null) |
json() when the request is JSON, post() otherwise. |
isJson(): bool |
Whether the Content-Type is application/json or a +json type. |
body(): string |
The raw body. |
headers(): array / header(string $name, ?string $default = null) |
Headers built from $_SERVER, with canonical names (Content-Type). Lookup is case-insensitive. getallheaders() is not needed. |
cookie(?string $key = null, mixed $default = null) |
A cookie value, or every cookie. |
file(string $key) / files() |
Uploaded files as UploadedFile objects. A multi-file field (name="photos[]") gives a list. |
secure(): bool |
Whether the request came over HTTPS. Proxy headers such as X-Forwarded-Proto are not trusted. |
server(string $key, mixed $default = null) |
A raw $_SERVER value. |
Missing values always return null or the $default you pass, never the whole array.
Uploaded files
$file = $request->file('avatar'); if ($file?->ok() && $file->size() < 2_000_000) { $file->moveTo('/var/app/storage/avatars/' . bin2hex(random_bytes(16)) . '.png'); }
UploadedFile provides ok(), error() (a UPLOAD_ERR_* constant), size(), path(), clientName(), clientType() and moveTo(string $destination). moveTo() uses move_uploaded_file(), so it refuses any file PHP didn't receive as an upload in this request.
The client's file name and MIME type are untrusted. Never build a path from them. clientName() already strips directory parts, but generate your own file names anyway.
Testing
composer install
composer test
Philosophy
- Small is Beautiful.
- Do one thing,
- Do one thing well.
- Get the PoC out ASAP.
- Consider your code dead two weeks after inception.
- n'joy!
sounds familiar? It should!