Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Modern HTTP handling with PHP - FOSDEM (BE)

Modern HTTP handling with PHP - FOSDEM (BE)

PSR-7 describes common interfaces for representing HTTP messages. HTTP messages are the foundation of web development. Web browsers and HTTP clients such as cURL create HTTP request messages that are sent to a web server, which provides an HTTP response message. Server-side code receives an HTTP request message, and returns an HTTP response message.

This talk will explain the interfaces defined by PSR-7, how they define the future of interoperability between frameworks and tools. After that there will be a showcase of several implementations and tools such as zend's diactoros package, Guzzle v6, php-http, RelayPHP and other packages that show the real power of shared interfaces for HTTP objects. A clear path to how we can start using these typed objects in our applications today will be shown.

Feedback here: https://joind.in/talk/ceecf

39eb3f3d313b13f05534e496285040b8?s=128

Hannes Van De Vreken

January 31, 2016
Tweet

Transcript

  1. HTTP messages Modern way of dealing with HTTP in PHP

    @hannesvdvreken @fosdem
  2. Hi, my name is Hannes.

  3. madewithlove.be

  4. PSR-7

  5. PSR-7 Common interfaces for HTTP messages

  6. 1. psr/http-message 2. guzzlehttp/guzzle 3. More PSR-7 usages

  7. 1. psr/http-message

  8. composer req symfony/http-foundation

  9. composer req psr/http-message PSR/HTTP-MESSAGE

  10. Differences with Symfony HTTP foundation: Interfaced PSR/HTTP-MESSAGE

  11. Differences with Symfony HTTP foundation: Interfaced Immutable PSR/HTTP-MESSAGE

  12. $newRequest = $request ->withHeader($name, $value); var_dump($newRequest === $request); > false

    PSR/HTTP-MESSAGE
  13. PSR/HTTP-MESSAGE What about existing Symfony Request/Response objects?

  14. PSR/HTTP-MESSAGE symfony/psr-http-message-bridge

  15. PSR/HTTP-MESSAGE Symfony request/response object ⁷ ⁷ PSR-7 request/response object

  16. PSR/HTTP-MESSAGE Symfony request/response object ⁷ ⁷ Zend request/response object \Psr\Http\Message\RequestInterface

    type hinted
  17. PSR/HTTP-MESSAGE let’s take a look

  18. PSR/HTTP-MESSAGE Recap Body: StreamInterface URI: UriInterface ServerRequestInterface ($_GET, $_POST, $_SERVER,

    $_COOKIES, $_FILES)
  19. PSR/HTTP-MESSAGE guzzlehttp/psr7 zendframework/zend-diactoros

  20. 2. guzzlehttp/guzzle

  21. GUZZLEHTTP/GUZZLE composer req guzzlehttp/guzzle

  22. GUZZLEHTTP/GUZZLE composer.json ^6.1

  23. semver.mwl.be

  24. GUZZLEHTTP/GUZZLE What’s new?

  25. GUZZLEHTTP/GUZZLE - WHAT’S NEW PSR-7

  26. GUZZLEHTTP/GUZZLE - PSR-7 guzzlehttp/psr7

  27. GUZZLEHTTP/GUZZLE - PSR-7 "provide": { "psr/http-message-implementation": "1.0" },

  28. None
  29. None
  30. GUZZLEHTTP/GUZZLE - PSR-7 use GuzzleHttp\Psr7\Request; $psrRequest = new Request( 'GET',

    $uri, $headers, $body );
  31. GUZZLEHTTP/GUZZLE - PSR-7 $client = new GuzzleHttp\Client(); $client->send($psrRequest);

  32. GUZZLEHTTP/GUZZLE - PSR-7 "require": { "php": ">=7.0.0", "guzzlehttp/psr7": "^1.1", }

  33. GUZZLEHTTP/GUZZLE - PSR-7 Demo https:/ /github.com/hannesvdvreken/psr7-demo/tree/master/1-guzzle

  34. GUZZLEHTTP/GUZZLE - WHAT’S NEW What else?

  35. GUZZLEHTTP/GUZZLE - PROMISES Promises

  36. GUZZLEHTTP/GUZZLE - PROMISES Not so new: (>=5.0.0) Promises

  37. GUZZLEHTTP/GUZZLE - PROMISES "require": { "php": ">=7.0.0", "guzzlehttp/psr7": "^1.1", "guzzlehttp/promise":

    "^1.0" }
  38. GUZZLEHTTP/GUZZLE - PROMISES use Psr\Http\Message\ResponseInterface as Res; $promise = $client->sendAsync($request);

    $promise->then(function (Res $response) { $response->getBody(); }); $promise->wait();
  39. GUZZLEHTTP/GUZZLE - PROMISES Demo https:/ /github.com/hannesvdvreken/psr7-demo/tree/master/2-promise https:/ /github.com/hannesvdvreken/psr7-demo/tree/master/3-multi

  40. GUZZLEHTTP/GUZZLE - WHAT’S NEW What else?

  41. GUZZLEHTTP/GUZZLE - EVENTING (>=4.0.0, <6.0.0) GuzzleHttp\Event\EmitterInterface GuzzleHttp\Event\SubscriberInterface

  42. GUZZLEHTTP/GUZZLE - MIDDLEWARES Now (>=6.0.0) Callable middlewares

  43. GUZZLEHTTP/GUZZLE - MIDDLEWARES $middleware = function ($handler) { return function

    ($request, $options) use ($handler) // Alter request, if you want return $handler($request, $options) ->then(function (ResponseInterface $respons // Alter response, if you want return $response; }); }; }
  44. GUZZLEHTTP/GUZZLE - MIDDLEWARES $stack = HandlerStack::create(); $client = new Client(['handler'

    => $stack]); $stack->push($middleware); $stack->unshift($middleware); $stack->remove($middleware);
  45. GUZZLEHTTP/GUZZLE - MIDDLEWARES $middleware = GuzzleHttp\Middleware::log( $psrLogger, new GuzzleHttp\MessageFormatter() );

  46. GUZZLEHTTP/GUZZLE - MIDDLEWARES Demo https:/ /github.com/hannesvdvreken/psr7-demo/tree/master/4-middleware

  47. GUZZLEHTTP/GUZZLE - RECAP Provides PSR-7 interfaced objects Middlewares instead of

    Events Async & Promises
  48. 3. More PSR-7 usages

  49. Interface for PSR-7 HTTP Clients

  50. Imagine you’re writing an SDK for GitHub/Slack/Hipchat/DO/…

  51. composer req guzzlehttp/guzzle

  52. None
  53. “I already use Zend’s HTTP client! ”

  54. “I can’t install Guzzle v5 ánd v6! ”

  55. PHP-HTTP/HTTPLUG - SOLUTION Decouple from the HTTP client

  56. None
  57. PHP-HTTP/HTTPLUG - SOLUTION interface HttpAdapter { public function sendRequest($request); public

    function sendRequests(array $requests); }
  58. PHP-HTTP/HTTPLUG - SOLUTION Virtual package "provide": {
 "php-http/adapter-implementation": "^0.1.0", }

  59. PHP-HTTP/HTTPLUG - ADAPTERS guzzle6-adapter guzzle5-adapter guzzle4-adapter guzzle3-adapter zend2-adapter zend1-adapter cake-adapter

    buzz-adapter react-adapter socket-adapter fopen-adapter file-get-contents-adapter native-curl-adapter pecl-adapter
  60. PHP-HTTP/HTTPLUG - COMPOSER.JSON "require": { "guzzlehttp/guzzle": "^5.0", "your-awesome/sdk": "^1.0",
 "php-http/guzzle5-adapter":

    "^0.1.0" }
  61. PHP-HTTP/HTTPLUG - COMPOSER.JSON "require": { - "guzzlehttp/guzzle": "^5.0", + "guzzlehttp/guzzle":

    "^6.0", "your-awesome/sdk": "^1.0",
 - "php-http/guzzle5-adapter": "^0.1.0" + "php-http/guzzle6-adapter": "^0.1.0" }
  62. PHP-HTTP/HTTPLUG - WRAPPING $github = new Github\Client( new Http\Adapter\Guzzle6HttpAdapter( new

    GuzzleHttp\Client() ) );
  63. None
  64. DECOUPLING - HTTP (PSR-7) - Logging (PSR-3) - Caching (PSR-6)

    - File system
  65. Middlewares wrapping HTTP applications

  66. None
  67. MIDDLEWARE DEFINITION function ( RequestInterface $request, ResponseInterface $response, callable $next

    ) {…};
  68. MIDDLEWARE DEFINITION function ($request, $response, $next) { // Alter request,

    if you want $response = $next($request, $response); // Alter response, if you want return $response; }
  69. MIDDLEWARE DEFINITION Session Logging Authentication Robots.txt file HTTP Cache Firewall

    Cors Throttling …
  70. MIGRATION FROM STACKPHP What about StackPHP (Symfony HTTP Kernel)?

  71. MIGRATION FROM STACKPHP h4cc/stack-psr7-bridge uses: symfony/psr-http-message-bridge

  72. MIGRATION FROM STACKPHP stack middleware ⁷ psr-7 middleware

  73. RECAP HTTP client decoupling Application middlewares

  74. RECAP 1. psr/http-message 2. guzzlehttp/guzzle 3. More PSR-7 usages

  75. Time for questions. @hannesvdvreken @fosdem

  76. Thank you! @hannesvdvreken @fosdem

  77. • http:/ /mwl.be • https:/ /github.com/guzzle/guzzle • https:/ /github.com/php-http •

    http:/ /docs.guzzlephp.org/en/latest/ • https:/ /github.com/php-fig/http-message • https:/ /github.com/php-fig/fig-standards/
 blob/master/accepted/PSR-7-http-message.md REFERENCES