http-message
The purpose of this PSR is to provide a set of common interfaces for HTTP messages as described in RFC 7230 and RFC 7231
Top Related Projects
PSR HTTP message bridge
Quick Overview
The php-fig/http-message repository contains common interface definitions for HTTP messages as defined by PSR-7. It provides a set of PHP interfaces for HTTP request and response objects, allowing for interoperability between different HTTP client libraries and frameworks.
Pros
- Standardizes HTTP message interfaces across PHP projects
- Improves interoperability between different PHP HTTP libraries and frameworks
- Encourages consistent API design for HTTP-related functionality
- Backed by the PHP Framework Interop Group (PHP-FIG), ensuring wide adoption
Cons
- Requires implementation by libraries and frameworks to be useful
- Some developers argue that the interfaces are too complex or verbose
- Doesn't provide concrete implementations, only interfaces
- May require additional effort to integrate with existing codebases
Code Examples
- Creating a ServerRequest object:
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\UriInterface;
$request = new ServerRequest(
'GET',
Uri::fromString('https://example.com/api'),
['Content-Type' => 'application/json'],
'{"key": "value"}',
'1.1',
['REMOTE_ADDR' => '127.0.0.1']
);
- Modifying a Response object:
use Psr\Http\Message\ResponseInterface;
$response = $response
->withStatus(200)
->withHeader('Content-Type', 'application/json')
->withBody(stream_for('{"result": "success"}'));
- Using a StreamInterface:
use Psr\Http\Message\StreamInterface;
$stream = $streamFactory->createStream('Hello, World!');
$content = $stream->getContents();
$stream->rewind();
$size = $stream->getSize();
Getting Started
To use PSR-7 interfaces in your project:
-
Install the package via Composer:
composer require psr/http-message
-
Use the interfaces in your code:
use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; class MyHttpClient { public function sendRequest(RequestInterface $request): ResponseInterface { // Implementation here } }
-
Implement the interfaces or use a library that provides concrete implementations (e.g., Guzzle, Laminas Diactoros, or Slim).
Competitor Comparisons
PSR HTTP message bridge
Pros of psr-http-message-bridge
- Provides a bridge between Symfony's HttpFoundation and PSR-7 interfaces
- Allows seamless integration of PSR-7 compliant libraries in Symfony applications
- Offers both PSR-7 to HttpFoundation and HttpFoundation to PSR-7 conversions
Cons of psr-http-message-bridge
- Adds an extra layer of abstraction, potentially impacting performance
- Requires additional dependencies compared to using http-message directly
- May introduce complexity in projects not using Symfony components
Code Comparison
http-message (PSR-7 interface):
interface RequestInterface extends MessageInterface
{
public function getRequestTarget();
public function withRequestTarget($requestTarget);
public function getMethod();
public function withMethod($method);
public function getUri();
}
psr-http-message-bridge (Usage example):
use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory;
use Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory;
$httpFoundationFactory = new HttpFoundationFactory();
$psrHttpFactory = new PsrHttpFactory($psr17Factory);
$symfonyRequest = $httpFoundationFactory->createRequest($psrRequest);
$psrRequest = $psrHttpFactory->createRequest($symfonyRequest);
The http-message repository defines PSR-7 interfaces, while psr-http-message-bridge provides implementation and conversion utilities between Symfony's HttpFoundation and PSR-7 compliant objects.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
PSR Http Message
This repository holds all interfaces/classes/traits related to PSR-7.
Note that this is not a HTTP message implementation of its own. It is merely an interface that describes a HTTP message. See the specification for more details.
Usage
Before reading the usage guide we recommend reading the PSR-7 interfaces method list:
Top Related Projects
PSR HTTP message bridge
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot