2015-08-04 phpnw ug two things to know about psr-7€¦ · a quick review of http request: {method}...

20
Two things to know about PSR-7 Rob Allen ~ August 2015

Upload: others

Post on 13-Aug-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 2015-08-04 PHPNW UG Two things to know about PSR-7€¦ · A quick review of HTTP Request: {METHOD} {URI} ... Another-Header: value Message body . A quick review of HTTP Response:

Two things to know about PSR-7

Rob Allen ~ August 2015

Page 2: 2015-08-04 PHPNW UG Two things to know about PSR-7€¦ · A quick review of HTTP Request: {METHOD} {URI} ... Another-Header: value Message body . A quick review of HTTP Response:

A quick review of HTTP

Request: {METHOD} {URI} HTTP:/1.1 Header: value1,value2 Another-Header: value

Message body

Page 3: 2015-08-04 PHPNW UG Two things to know about PSR-7€¦ · A quick review of HTTP Request: {METHOD} {URI} ... Another-Header: value Message body . A quick review of HTTP Response:

A quick review of HTTP

Response: HTTP:/1.1 {STATUS CODE} {REASON PHRASE} Header: value1,value2 Another-Header: value

Message body

Page 4: 2015-08-04 PHPNW UG Two things to know about PSR-7€¦ · A quick review of HTTP Request: {METHOD} {URI} ... Another-Header: value Message body . A quick review of HTTP Response:

How does PHP do it?

Request • Superglobals • php://input

Page 5: 2015-08-04 PHPNW UG Two things to know about PSR-7€¦ · A quick review of HTTP Request: {METHOD} {URI} ... Another-Header: value Message body . A quick review of HTTP Response:

How does PHP do it?

Response: • header() • echo (& ob_*() methods)

Page 6: 2015-08-04 PHPNW UG Two things to know about PSR-7€¦ · A quick review of HTTP Request: {METHOD} {URI} ... Another-Header: value Message body . A quick review of HTTP Response:

PSR-7

It’s just some interfaces • Request & ServerRequest • Response

Page 7: 2015-08-04 PHPNW UG Two things to know about PSR-7€¦ · A quick review of HTTP Request: {METHOD} {URI} ... Another-Header: value Message body . A quick review of HTTP Response:

PSR-7

It’s just some interfaces • Request & ServerRequest • Response • Header • Stream

Page 8: 2015-08-04 PHPNW UG Two things to know about PSR-7€¦ · A quick review of HTTP Request: {METHOD} {URI} ... Another-Header: value Message body . A quick review of HTTP Response:

PSR-7

It’s just some interfaces • Request & ServerRequest • Response • Header • Stream • Uri • UploadFile

Page 9: 2015-08-04 PHPNW UG Two things to know about PSR-7€¦ · A quick review of HTTP Request: {METHOD} {URI} ... Another-Header: value Message body . A quick review of HTTP Response:

Now, we come to the two important things!

Page 10: 2015-08-04 PHPNW UG Two things to know about PSR-7€¦ · A quick review of HTTP Request: {METHOD} {URI} ... Another-Header: value Message body . A quick review of HTTP Response:

Immutable

Request, Response, Uri & UploadFile are immutable

$uri = new Uri('https://api.joind.in/v2.1/events');

Page 11: 2015-08-04 PHPNW UG Two things to know about PSR-7€¦ · A quick review of HTTP Request: {METHOD} {URI} ... Another-Header: value Message body . A quick review of HTTP Response:

Immutable

Request, Response, Uri & UploadFile are immutable

$uri = new Uri('https://api.joind.in/v2.1/events'); $uri = $uri->withQuery('?filter=upcoming');

Page 12: 2015-08-04 PHPNW UG Two things to know about PSR-7€¦ · A quick review of HTTP Request: {METHOD} {URI} ... Another-Header: value Message body . A quick review of HTTP Response:

Immutable

Request, Response, Uri & UploadFile are immutable

$uri = new Uri('https://api.joind.in/v2.1/events'); $uri = $uri->withQuery('?filter=upcoming'); $request = (new Request()) ->withMethod('GET') ->withUri($uri) ->withHeader('Accept', 'application/json') ->withHeader('Authorization', 'Bearer 0873418d');

Page 13: 2015-08-04 PHPNW UG Two things to know about PSR-7€¦ · A quick review of HTTP Request: {METHOD} {URI} ... Another-Header: value Message body . A quick review of HTTP Response:

Streams

Message bodies are streams

$image = __DIR__ . ‘/huge_photo.jpg'; $body = new Stream($image);

$response = (new Response()) ->withStatus(200, 'OK') ->withHeader('Content-Type', 'image/jpeg') ->withHeader(‘Content-Length', filesize($image)) ->withBody($body);

Page 14: 2015-08-04 PHPNW UG Two things to know about PSR-7€¦ · A quick review of HTTP Request: {METHOD} {URI} ... Another-Header: value Message body . A quick review of HTTP Response:

Streams

Text is common - use write()

$body = new Stream(); $body->write(json_encode(['foo' => 'bar']));

$response = (new Response()) ->withStatus(200, 'OK') ->withHeader('Content-Type', 'application/json') ->withHeader('Accept', 'application/json') ->withBody($body);

Page 15: 2015-08-04 PHPNW UG Two things to know about PSR-7€¦ · A quick review of HTTP Request: {METHOD} {URI} ... Another-Header: value Message body . A quick review of HTTP Response:

Streams

Writable streams are mutable

$body = new Stream(); $body->write(‘Hello '); $body->write(‘World!');

$response = (new Response()) ->withStatus(200, 'OK') ->withHeader('Content-Type', 'text/plain') ->withBody($body);

Page 16: 2015-08-04 PHPNW UG Two things to know about PSR-7€¦ · A quick review of HTTP Request: {METHOD} {URI} ... Another-Header: value Message body . A quick review of HTTP Response:

Why PSR-7?

Page 17: 2015-08-04 PHPNW UG Two things to know about PSR-7€¦ · A quick review of HTTP Request: {METHOD} {URI} ... Another-Header: value Message body . A quick review of HTTP Response:

Interoperability

Page 18: 2015-08-04 PHPNW UG Two things to know about PSR-7€¦ · A quick review of HTTP Request: {METHOD} {URI} ... Another-Header: value Message body . A quick review of HTTP Response:

Interoperability

$file = $s3Client->getObject(/*…*/]);

return $response->withBody($file['body']);

Page 19: 2015-08-04 PHPNW UG Two things to know about PSR-7€¦ · A quick review of HTTP Request: {METHOD} {URI} ... Another-Header: value Message body . A quick review of HTTP Response:

• Immutable objects• Body is a stream

Remember this!

Page 20: 2015-08-04 PHPNW UG Two things to know about PSR-7€¦ · A quick review of HTTP Request: {METHOD} {URI} ... Another-Header: value Message body . A quick review of HTTP Response:

Thank you

Rob Allen ~ http://akrabat.com ~ @akrabat