psr/http-factory
PSR-17 HTTP message factory interfaces for creating requests, responses, streams, URIs, and uploaded files. This package provides only the standard interfaces (no implementation). Find compatible implementations on Packagist.
Start by installing a factory implementation—psr/http-factory itself is only interfaces, not a concrete package. Popular implementations include nyholm/psr7 (lightweight, no dependencies) or laminas/laminas-diactoros. Once installed, configure your HTTP client or framework (e.g., Symfony HttpClient, Guzzle, Laravel HTTP client) to use the implementation. Your first use case will be creating PSR-7 messages:
use Nyholm\Psr7\Factory\Psr17Factory;
$factory = new Psr17Factory();
$request = $factory->createRequest('GET', 'https://api.example.com/users');
$response = $factory->createResponse(200)->withHeader('Content-Type', 'application/json');
Begin in controller/service classes where you manually construct requests/responses—especially for API clients or CLI tools.
Psr\Http\Factory\RequestFactoryInterface or ResponseFactoryInterface into services instead of concrete factories—keeps code decoupled and testable.Laminas\Diactoros\Message::fromGlobals() or Symfony\Component\HttpFoundation\Request::createFromGlobals()) only for low-level bootstrapping; switch to factory interfaces for business logic. Illuminate\Http\Request::createFromBase() or ResponseFactory can wrap PSR-7, but prefer native Laravel types unless interoperating with PSR-7 clients. For HTTP clients (e.g., Guzzle), rely on GuzzleHttp\Psr7’s factory implementation.withXxx() chains (e.g., ->withHeader(), ->withBody()) to assert request/response modifications.new-able classes—ensure your composer.json requires an implementation (e.g., "nyholm/psr7": "^1.5"). Check with composer show psr/http-factory-implementation.RequestFactory vs ResponseFactory: createRequest() accepts a UriInterface|string or a full UriInterface—but always prefer passing UriInterface (e.g., $factory->createUri('https://...')) to avoid accidental URL parsing bugs.withXxx() calls (e.g., $req->withHeader(...)) is a common bug. Enable static analysis (e.g., PHPStan) to catch this.nyholm/psr7 v1.5+ does; older versions may fail).(string) $request->getUri() or json_encode($request->getHeaders()) to inspect request state—__toString() is not guaranteed on objects.How can I help you explore Laravel packages today?