Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

PSR-7 HTTP Messages

972 views

Published on

I did this talk on a PHP meetup in Stockhom, 2015-10-07

Published in: Internet
  • Be the first to comment

PSR-7 HTTP Messages

  1. 1. PSR-7 Tobias Nyholm
  2. 2. PHP 7
  3. 3. PSR-7 from PHP-FIG
  4. 4. PHP-Fig
  5. 5. Standards PSR-0 PSR-1 PSR-2 PSR-3 PSR-4 PSR-5 PSR-6 PSR-7 PSR-8 PSR-9 PSR-10 Autoloading Basic coding standard Coding style Logger Autoloading PHPDoc (draft) Caching (review) HTTP Messages Huggable interface (draft) Security Disclosure (draft) Security Advisories (draft)
  6. 6. PSR-8
  7. 7. About me • Tobias Nyholm, @tobiasnyholm • Happyr.com • Certified Symfony developer • Your organizer
  8. 8. PSR-7 Standard interfaces for HTTP messages
  9. 9. Why do we need PSR-7? We could write framework agnostic code like: • Routers • Controllers • Cache • Logging • Authentication • etc…
  10. 10. GET /startpage HTTP/1.1 Host: xkcd.com Accept: text/html User-Agent: Mozilla/5.0 (Macintosh)
  11. 11. HTTP/1.1 200 OK Date: Sat, 02 Apr 2011 21:05:05 GMT Server: lighttpd/1.4.19 Content-Type: text/html <html> <!-- ... HTML for the xkcd comic --> </html>
  12. 12. In PHP
  13. 13. The interfaces • MessageInterface • ResponseInterface • RequestInterface • ServerRequestInterface • UriInterface • StreamInterface • UploadedFileInterface
  14. 14. Client request POST /send-message HTTP/1.0 Location: www.example.com Content-Type: application/x-www-form-urlencoded Content-Length: 33 from=Tobias&to=all&message=hello MessageInterface RequestInterface UriInterface
  15. 15. What to care about? RequestInterface getMethod getRequestTarget getUri getProtocolVersion getHeaders getHeader hasHeader getBody ResponseInterface getStatusCode getReasonPhrase getProtocolVersion getHeaders getHeader hasHeader getBody
  16. 16. Getting headers
  17. 17. PSR-7 objects are immutable
  18. 18. Mutable vs immutable • Mutable - edit • Immutable - read only
  19. 19. No setters RequestInterface withMethod withRequestTarget withUri withProtocolVersion withHeader withAddedHeader withoutHeader withBody ResponseInterface withStatus withProtocolVersion withHeader withAddedHeader withoutHeader withBody
  20. 20. DateTime is mutable
  21. 21. Don’t do this
  22. 22. With*
  23. 23. Streams
  24. 24. Streams StreamInterface __toString isReadable isSeekable isWriteable getSize read write seek rewind close … Use it as it was a string
  25. 25. Middleware
  26. 26. Add header to all responses
  27. 27. Adapters
  28. 28. All adapters in one place • PHP-HTTP • http://php-http.readthedocs.org/en/latest/ • Will replace Ivory
 (https://github.com/egeloen/ivory-http-adapter)
  29. 29. Composer.json
  30. 30. Start using it
  31. 31. Read more at • http://www.php-fig.org/psr/psr-7/ • https://github.com/php-http • https://github.com/oscarotero/psr7-middlewares • https://github.com/Happyr/HttpAutoDiscovery

×