Pro Yearly is on sale from $80 to $50! »

Modern HTTP handling with PHP - Dutch PHP conference

Modern HTTP handling with PHP - Dutch PHP conference

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.

Feedback here: https://joind.in/talk/1786a

39eb3f3d313b13f05534e496285040b8?s=128

Hannes Van De Vreken

June 25, 2016
Tweet

Transcript

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

    @hannesvdvreken @dpcon #dpc16
  2. Hi, my name is Hannes.

  3. !

  4. madewithlove.be 20 × !%&'()

  5. None
  6. PSR-7

  7. PSR-7 Common interfaces for HTTP messages

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

  9. 1. psr/http-message

  10. Common interfaces for HTTP messages

  11. Common interfaces for HTTP messages - Works in any Framework

    - Both for HTTP Clients and HTTP applications - HTTP version independent
  12. Common interfaces for HTTP messages

  13. Pre-PSR-7

  14. composer req symfony/http-foundation

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

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

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

    PSR/HTTP-MESSAGE
  18. PSR/HTTP-MESSAGE let’s take a look

  19. PSR/HTTP-MESSAGE - RECAP MessageInterface RequestInterface ResponseInterface StreamInterface❗ UriInterface ServerRequestInterface ($_GET,

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

  21. 2. guzzlehttp/guzzle

  22. GUZZLEHTTP/GUZZLE composer req guzzlehttp/guzzle

  23. GUZZLEHTTP/GUZZLE composer.json ^6.2

  24. semver.mwl.be

  25. GUZZLEHTTP/GUZZLE What’s new?

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

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

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

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

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

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

  34. GUZZLEHTTP/GUZZLE - PROMISES Promises

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

  36. GUZZLEHTTP/GUZZLE - PSR-7 guzzlehttp/promise

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

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

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

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

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

  42. 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; }); }; }
  43. GUZZLEHTTP/GUZZLE - MIDDLEWARES use GuzzleHttp\Middleware; $middleware = Middleware::mapRequest( function (RequestInterface

    $request) { // Modify request return $request; } );
  44. GUZZLEHTTP/GUZZLE - MIDDLEWARES use GuzzleHttp\Middleware; $middleware = Middleware::mapResponse( function (ResponseInterface

    $request) { // Modify response return $response; } );
  45. GUZZLEHTTP/GUZZLE - MIDDLEWARES $middleware = GuzzleHttp\Middleware::log( $psr3Logger, new GuzzleHttp\MessageFormatter() );

  46. GUZZLEHTTP/GUZZLE - MIDDLEWARES $stack = HandlerStack::create(); $stack->push($middleware); $stack->unshift($middleware); $stack->remove($middleware); $client

    = new Client(['handler' => $stack]);
  47. GUZZLEHTTP/GUZZLE - MIDDLEWARES Demo https:/ /github.com/hannesvdvreken/psr7-demo/tree/master/4-middleware

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

    Eventing Async & Promises
  49. 3. More PSR-7 usages

  50. Decoupling HTTP Clients

  51. PHP-HTTP/HTTPLUG - SOLUTION php-http/httplug

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

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

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

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

    "^0.1.0" }
  56. 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" }
  57. START USING PSR-7 IN YOUR APP What about existing Symfony

    Request/Response objects?
  58. START USING PSR-7 IN YOUR APP symfony/psr-http-message-bridge

  59. START USING PSR-7 IN YOUR APP Symfony request/response object ⁷

    ⁷ PSR-7 request/response object
  60. START USING PSR-7 IN YOUR APP Symfony request/response object ⁷

    ⁷ Zend request/response object \Psr\Http\Message\RequestInterface type hinted
  61. Middlewares wrapping HTTP applications

  62. None
  63. MIDDLEWARE DEFINITION function ( RequestInterface $request, ResponseInterface $response, callable $next

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

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

    Cors Throttling …
  66. MIDDLEWARE DEFINITION Build your stack with: relay/relay mindplay/middleman

  67. MIGRATION FROM STACKPHP What about StackPHP (Symfony HTTP Kernel)?

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

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

  70. MIGRATION FROM STACKPHP Demo https:/ /github.com/hannesvdvreken/psr7-demo/tree/master/6-middleware

  71. HTTP MIDDLEWARES PSR-15 will change that

  72. HTTP MIDDLEWARES PSR-15 is not accepted yet

  73. HTTP MIDDLEWARES - PSR-15 MiddlewareInterface > ClientMiddlewareInterface > ServerMiddlewareInterface FrameInterface

    StackInterface
  74. HTTP MIDDLEWARES https:/ /github.com/php-fig/fig-standards/ blob/master/proposed/http-middleware/ middleware.md

  75. RECAP HTTP client decoupling Symfony objects to PSR-7 objects Application

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

  77. Thank you! https://joind.in/talk/1786a @hannesvdvreken @dpcon #dpc16

  78. Time for questions. @hannesvdvreken @dpcon #dpc16

  79. • 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