-
Notifications
You must be signed in to change notification settings - Fork 0
Quick Start
A ten-minute tour of every layer the package ships, with copy-pasteable code.
use InitPHP\HTTP\Message\Request;
$request = new Request(
'POST', // method
'https://api.example.com/users', // URI as string or UriInterface
['Accept' => 'application/json'], // headers — string or list-of-strings
json_encode(['name' => 'Ada']), // body — string|resource|StreamInterface|null
'1.1' // HTTP protocol version
);Every PSR-7 message is immutable:
$updated = $request->withHeader('X-Trace-Id', '0f1e2d');
// $request still has no X-Trace-Id; $updated doesSee Request for the full surface.
use InitPHP\HTTP\Message\Response;
$response = (new Response(200))
->withHeader('Content-Type', 'text/plain; charset=utf-8');
$response->getBody()->write('Hello!');Two convenience producers ship on Response for the cases that come up most often:
$response = (new Response())->json(['ok' => true], 200);
$response = (new Response())->redirect('https://example.com/welcome', 302);See Response, Recipe — JSON Response and Recipe — Redirect.
use InitPHP\HTTP\Client\Client;
use InitPHP\HTTP\Message\Request;
$client = (new Client())
->withTimeout(10)
->withConnectTimeout(3)
->withUserAgent('my-app/1.0');
$response = $client->sendRequest(
new Request('GET', 'https://httpbin.org/uuid')
);
echo $response->getStatusCode(); // 200
echo (string) $response->getBody(); // {"uuid":"..."}There are higher-level helpers when you don't want to build a Request by hand:
$client->get('https://api.example.com/users');
$client->post('https://api.example.com/users', '{"name":"Ada"}', [
'Content-Type' => 'application/json',
]);
$client->fetch('https://api.example.com/users', [
'method' => 'POST',
'body' => json_encode(['name' => 'Ada']),
'headers' => ['Content-Type' => 'application/json'],
]);PSR-18 contract is honoured: 4xx and 5xx responses are returned, not thrown. Only transport-level failures raise exceptions. See Client Exceptions.
use InitPHP\HTTP\Message\ServerRequest;
$request = ServerRequest::createFromGlobals();
$request->getMethod(); // "GET", "POST", ...
$request->getUri()->getPath();
$request->getQueryParams(); // $_GET
$request->getParsedBody(); // automatically parsed for JSON / urlencoded forms
$request->getUploadedFiles(); // normalised UploadedFile tree
$request->getAttribute('user');The factory is stateless — each call returns a fresh instance. See ServerRequest.
use InitPHP\HTTP\Emitter\Emitter;
(new Emitter())->emit($response);
// For multi-MB bodies, stream in chunks:
(new Emitter())->emit($response, 65536);The emitter writes the status line, headers and body in the order any reverse proxy expects. See Emitter, Chunked Bodies and Content-Range.
use InitPHP\HTTP\Facade\Factory;
use InitPHP\HTTP\Facade\Client;
use InitPHP\HTTP\Facade\Emitter;
$request = Factory::createRequest('GET', 'https://example.com');
$response = Client::sendRequest($request);
Emitter::emit($response);Each facade lazily resolves a singleton on first call. See Facades.
use InitPHP\HTTP\Message\Response;
use InitPHP\HTTP\Message\ServerRequest;
use InitPHP\HTTP\Emitter\Emitter;
$request = ServerRequest::createFromGlobals();
if ($request->getMethod() !== 'POST') {
$response = (new Response(405, ['Allow' => 'POST']))
->withHeader('Content-Type', 'text/plain');
$response->getBody()->write('Method Not Allowed');
} else {
$body = $request->getParsedBody() ?? [];
$response = (new Response())->json([
'received' => $body,
'remote' => $request->getServerParams()['REMOTE_ADDR'] ?? null,
], 201);
}
(new Emitter())->emit($response);- Type-hint against PSR-7? Read PSR-7 Overview.
- Production-harden the Client? Read Client Configuration.
- Receive file uploads? Read UploadedFile.
- Coming from v2.x? Read the Migration Guide.
initphp/http · MIT License · part of the InitPHP family
Source · Issues · Discussions · Packagist · Contributing · Security Policy
Getting Started
PSR-7 Messages
PSR-17 Factories
PSR-18 Client
Emitter (SAPI)
Static Facades
Recipes
Reference
Migration & Help