What is PSR-7 and how to use it
PSR-7 is a set of common interfaces defined by PHP Framework Interop Group. These interfaces are representing HTTP messages, and URIs for use when communicating trough HTTP.
Any web application using this set of interfaces is a PSR-7 application.
More about interfaces and interfaces examples can be found here.
Interfaces
In this section the Interfaces methods will be listed. The purpose of this list is to help in finding the methods when working with PSR-7. This can be considered as a cheatsheet for PSR-7 interfaces. The interfaces defined in PSR-7 are the following:
| Class Name | Description |
|---|---|
| PsrHttpMessageMessageInterface | Representation of a HTTP message |
| PsrHttpMessageRequestInterface | Representation of an outgoing, client-side request. |
| PsrHttpMessageServerRequestInterface | Representation of an incoming, server-side HTTP request. |
| PsrHttpMessageResponseInterface | Representation of an outgoing, server-side response. |
| PsrHttpMessageStreamInterface | Describes a data stream |
| PsrHttpMessageUriInterface | Value object representing a URI. |
| PsrHttpMessageUploadedFileInterface | Value object representing a file uploaded through an HTTP request. |
Working with PSR-7
The following examples will illustrate how basic operations are done in PSR-7.
Zend Diactoros is an implementation for PSR-7 interfaces. It will be used to illustrate these examples. Installation guide for Zend Diactoros: Zend Diactoros Documentation – Installation
All other PSR-7 implementations should have the same behaviour.
Alternative PSR-7 implementations.
To use the Zend Diactoros classes add this at the beggining of the php file:
>use ZendDiactorosServerRequestFactory; use ZendDiactorosResponse; // autoloading $request = ServerRequestFactory::fromGlobals($_SERVER, $_GET, $_POST, $_COOKIE, $_FILES); $response = new Response();
Note: The article applies to all PSR-7 implementations from this point forward.
Working with HTTP Headers
Adding headers to response:
>$response->withHeader('My-Custom-Header', 'My Custom Message');
Appending values to headers
>$response->withAddedHeader('My-Custom-Header', 'The second message');
Checking if header exists:
>$response->hasHeader('My-Custom-Header'); // will return true
Note: My-Custom-Header was only added in the Response
Getting comma-separated values from a header (also applies to request)
>// getting value from request headers
$request->getHeaderLine('Content-Type'); // will return: "text/html; charset=UTF-8"
// getting value from response headers
$response->getHeaderLine('My-Custom-Header'); // will return: "My Custom Message; The second message"
>// getting value from request headers
$request->getHeaderLine('Content-Type'); // will return: "text/html; charset=UTF-8"
// getting value from response headers
$response->getHeaderLine('My-Custom-Header'); // will return: "My Custom Message; The second message"
Getting array of value from a header (also applies to request)
>// getting value from request headers
$request->getHeader('Content-Type'); // will return:
// getting value from response headers
$response->getHeader('My-Custom-Header'); // will return:
Removing headers from HTTP Messages
>// removing a header from Request, removing deprecated "Content-MD5" header
$request->withoutHeader('Content-MD5');
// removing a header from Response
// effect: the browser won't know the size of the stream
// the browser will download the stream till it ends
$response->withoutHeader('Content-Length');
Working with HTTP Message Body
When working with the PSR-7 there are two methods of implementation:
1. Getting the body separately
This method makes the body handling easier to understand and is useful when repeatedly calling body methods. (You only call
getBody()once). Using this method mistakes like$response->write()are also prevented.
>$body = $response->getBody(); // operations on body, eg. read, write, seek // ... // replacing the old body $response->withBody($body); // this last statement is optional as we working with objects // in this case the "new" body is same with the "old" one // the $body variable has the same value as the one in $request, only the reference is passed
2. Working directly on response
This method is useful when only performing few operations as the
$request->getBody()statement fragment is required
>$response->getBody()->write('hello');
Getting the body contents
The following snippet gets the contents of a stream contents.
Note: Streams must be rewinded, if content was written into streams, it will be ignored when calling
getContents()because the stream pointer is set to the last character, which is