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

Modern HTTP handling with PHP - Istanbul PHP

Modern HTTP handling with PHP - Istanbul PHP

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/87f8c

39eb3f3d313b13f05534e496285040b8?s=128

Hannes Van De Vreken

May 22, 2016
Tweet

More Decks by Hannes Van De Vreken

Other Decks in Technology

Transcript

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

    @hannesvdvreken @istanbulphp
  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 - 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 - PSR-7 guzzlehttp/promise

  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 use GuzzleHttp\Middleware; $middleware = Middleware::mapRequest( function (RequestInterface

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

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

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

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

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

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

  51. Decoupling HTTP Clients

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

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

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

  55. 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
  56. PHP-HTTP/HTTPLUG - COMPOSER.JSON "require": { "guzzlehttp/guzzle": "^5.0", "your-awesome/sdk": "^1.0",
 "php-http/guzzle5-adapter":

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  74. Time for questions. @hannesvdvreken @istanbulphp

  75. Thank you! https://joind.in/talk/87f8c @hannesvdvreken @istanbulphp

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