fperdomo / http
HTTP Component
Installs: 674
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 19
pkg:composer/fperdomo/http
Requires
- php: >=8.3
- nyholm/psr7: ^1.8
- psr/http-factory: ^1.1
- psr/http-message: ^2.0
- psr/http-server-handler: ^1.0
- psr/http-server-middleware: ^1.0
Requires (Dev)
- phpunit/phpunit: 12.3.*
- rector/rector: ^2.1
This package is auto-updated.
Last update: 2026-01-26 00:17:53 UTC
README
GitHub repository: https://github.com/masterfermin02/http
A modern, lightweight HTTP component for PHP 8.3+ with full PSR-7, PSR-17, and PSR-15 interoperability.
Features
Simple & Intuitive API - Clean object-oriented wrappers around PHP superglobals
PSR-7 Compatible - HTTP message interfaces for request/response handling
PSR-17 Factory Support - Standard factory methods for creating HTTP objects
PSR-15 Middleware - Full middleware and request handler support
Modern PHP - Built for PHP 8.3+ with typed properties and readonly classes
Framework Agnostic - Works with any PHP framework or standalone
Why This Package?
This is a maintained fork that brings modern PHP 8.3+ support and PSR interoperability to a proven HTTP component. The original package hasn't been updated since 2016, but this version is actively maintained with:
- PHP 8.3+ support with modern type hints
- PSR-7, PSR-17, and PSR-15 adapters for ecosystem compatibility
- Continuous integration and automated testing
- Regular updates and community support
Installation
composer require fperdomo/http
Quick Start
Traditional Approach (Simple & Direct)
use Http\HttpRequest; use Http\HttpResponse; $request = new HttpRequest($_GET, $_POST, $_COOKIE, $_FILES, $_SERVER, file_get_contents('php://input')); $response = new HttpResponse; $name = $request->getParameter('name', 'Guest'); $response->setContent("Hello, {$name}!"); $response->addHeader('Content-Type', 'text/html'); foreach ($response->getHeaders() as $header) { header($header, false); } echo $response->getContent();
PSR-7 Approach (Framework Interoperability)
use Http\Psr\Psr7RequestFactory; use Http\Psr\Psr7ResponseFactory; // Create PSR-7 compliant objects $factory = new Psr7RequestFactory(); $request = $factory->createServerRequestFromGlobals(); $responseFactory = new Psr7ResponseFactory(); $response = $responseFactory->createResponse(200) ->withHeader('Content-Type', 'application/json'); $response->getBody()->write(json_encode(['status' => 'success']));
PSR-15 Middleware (Modern Architecture)
use Http\Psr\Psr15Handler; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ResponseInterface; class AuthMiddleware implements MiddlewareInterface { public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface { // Authentication logic here return $handler->handle($request); } } $handler = new Psr15Handler(); $handler->addMiddleware(new AuthMiddleware()); $response = $handler->handle($request);
PSR Interoperability
PSR-7: HTTP Message Interfaces
Use the provided adapters to convert between native objects and PSR-7 interfaces:
use Http\HttpRequest; use Http\HttpResponse; // Convert native request to PSR-7 $nativeRequest = new HttpRequest($_GET, $_POST, $_COOKIE, $_FILES, $_SERVER, file_get_contents('php://input')); $psr7Request = $nativeRequest->toPsrServerRequest(); // Convert native response to PSR-7 $nativeResponse = new HttpResponse(); $psr7Response = $nativeResponse->toPsrResponse(); // Convert PSR-7 response back to native $httpResponse = HttpResponse::fromPsrResponse($psr7Response);
📚 Complete PSR Interoperability Guide | 🚀 Quick Start
PSR-17: HTTP Factories
Create HTTP objects using standard factory interfaces:
use Http\Psr\Psr17Factory; $factory = new Psr17Factory(); // Request factories $request = $factory->createRequest('GET', 'https://example.com'); $serverRequest = $factory->createServerRequestFromGlobals(); // From superglobals // Response factory $response = $factory->createResponse(200, 'OK'); // Message component factories $stream = $factory->createStream('Hello World'); $uri = $factory->createUri('https://example.com/path');
PSR-15: Middleware & Request Handlers
Build middleware pipelines compatible with any PSR-15 framework:
use Http\Psr\MiddlewarePipeline; use Psr\Http\Server\MiddlewareInterface; use Psr\Http\Server\RequestHandlerInterface; $pipeline = new MiddlewarePipeline(); // Add middleware $pipeline->pipe(new AuthenticationMiddleware()); $pipeline->pipe(new LoggingMiddleware()); $pipeline->pipe($finalHandler); // Process request through pipeline $response = $pipeline->handle($serverRequest);
💡 See examples/psr-interop.php for complete working examples.
Cookies
To avoid new calls in your classes and to have the ability to set default cookie settings for you application, there is a CookieBuilder class that you can use to create your cookie objects. It has the following methods available:
$cookieBuilder->setDefaultDomain($domain); // defaults to NULL $cookieBuilder->setDefaultPath($path); // defaults to '/' $cookieBuilder->setDefaultSecure($secure); // defaults to TRUE $cookieBuilder->setDefaultHttpOnly($httpOnly); // defaults to TRUE $cookieBuilder->build($name, $value); // returns the cookie object
You can use the following methods to manipulate an existing cookie:
$cookie->setValue($value); $cookie->setMaxAge($seconds); $cookie->setDomain($domain); $cookie->setPath($path); $cookie->setSecure($secure); $cookie->setHttpOnly($httpOnly);
The cookie object can the be used with the HttpResponse methods addCookie and deleteCookie.
Example
<?php use Http\HttpRequest; use Http\HttpResponse; use Http\CookieBuilder; $loader = require_once __DIR__ . '/vendor/autoload.php'; $cookieBuilder = new CookieBuilder; // Disable the secure flag because this is only an example $cookieBuilder->setDefaultSecure(false); $request = new HttpRequest($_GET, $_POST, $_COOKIE, $_FILES, $_SERVER, file_get_contents('php://input')); $response = new HttpResponse; $content = '<h1>Hello World</h1>'; $content .= $request->getCookie('TestCookie', 'Cookie is not set.'); if ($request->getParameter('setCookie') === 'true') { $cookie = $cookieBuilder->build('TestCookie', 'Cookie is set.'); $response->addCookie($cookie); } $response->setContent($content); foreach ($response->getHeaders() as $header) { header($header); } echo $response->getContent();