tekst
PSR-7 - HTTP message interfacesPaweł Skotnicki
About me
PHP developer @ The Software House
PHPers Silesia co-organizer
BDD/DDD enthusiast
PHP-FIG ?
PHP Framework Interop Group ?
PHP Standard Recommendation ?
PSR ?
PHP-FIG
PHP Framework Interop Group
40+ members
PSR
PSR-0 Autoloading Standard
PSR-1 Basic Coding Standard
PSR-2 Coding Style Guide
PSR-3 Logger Interface
PSR-4 Autoloading Standard
PSR-5 PHPDoc Standard(Draft)
PSR-6 Caching Interface(Review)
PSR-7 HTTP message interfaces
HTTP request
POST /path HTTP/1.1Host: example.com
foo=bar&baz=bat
HTTP response
HTTP/1.1 200 OKContent-Type: text/plain
This is the response body
PSR-7
http://www.php-fig.org/psr/psr-7/
https://github.com/php-fig/http-message
composer require psr/http-message
namespace Psr\Http\Message
Psr\Http\MessageMessageInterface
ResponseInterface
RequestInterface
ServerRequestInterface
UriInterface
StreamInterface
UploadedFileInterface
MessageInterface
immutable value objects
protocol version
headers
body
HTTP Message Headers$message->getHeader('Accept'); //array$message->getHeaderLine('Accept'); //string$message->hasHeader('Accept'); //boolean
/* ['Header’ => ['value1', 'value2']]*/$message->getHeaders();
HTTP Message Headers$message = $message->withHeader('X-Foo', 'bar');$message = $message->withAddedHeader( 'X-Foo', 'baz' );
$message = $message->withoutHeader('X-Foo');
HTTP Message Body
StreamInterface
may be mutable !
ResponseInterface
$response->getStatusCode();$response->getReasonPhrase();
$response = $response->withStatus(200, 'OK');
RequestInterface
method
request-target
uri
UriInterface
request-target
origin-form
absolute-form
authority-form
asterisk-form
RequestInterface
<get/with>Method
<get/with>RequestTarget
<get/with>Uri
ServerRequestInterfacegetServerParams ($_SERVER)
<get/with>CookieParams ($_COOKIE)
<get/with>QueryParams ($_GET)
<get/with>UploadedFiles ($_FILES)
<get/with>ParsedBody ($_POST)
attributes
Uploaded Files
UploadedFileInterface
getStream() // StreamInterface
moveTo()
size, error, client filename, client media type
getUploadedFiles() - normalized structure
Single file upload
<input type="file" name="avatar" />
$_FILES[ 'avatar' => [ 'tmp_name' => 'phpUxcOty', 'name' => 'my-avatar.png', 'size' => 90996, 'type' => 'image/png', 'error' => 0, ],]
getUploadedFiles()
[ 'avatar' => /* UploadedFileInterface instance */]
Multiple files upload
<input type="file" "name="my-form[avatars][]" /><input type="file" "name="my-form[avatars][]" />
$_FILES[ 'my-form' => [ 'avatars' => [ 'tmp_name' => [ 0 => '...', 1 => '...', ], 'name' => […], 'size' => […], 'type' => […], 'error' => […], ], ],]
getUploadedFiles()
[ 'my-form' => [ 'avatars' => [ 0 => /* UploadedFileInterface instance */, 1 => /* UploadedFileInterface instance */, ], ],]
Use cases
Clients
unified request and response interfaces
Middleware
Frameworks
Who ?
Guzzle, PHP HTTP client
Zend Diactoros, PSR HTTP Message implementation
PSR HTTP Message Bridge (Symfony 2)
Relay, PSR-7 middleware dispatcher
Aura Router
Questions?
Thank you