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

Modern HTTP handling with PHP - Meetup PHP Limburg (BE)

Modern HTTP handling with PHP - Meetup PHP Limburg (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/view/16461

39eb3f3d313b13f05534e496285040b8?s=128

Hannes Van De Vreken

November 25, 2015
Tweet

Transcript

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

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

  3. madewithlove.be

  4. None
  5. PSR-7

  6. PSR-7 Common interfaces for HTTP messages

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

  8. 1. psr/http-message

  9. Differences with Symfony objects: Interfaces, not concretions PSR/HTTP-MESSAGE

  10. Differences with Symfony objects: Interfaces, not concretions Immutable PSR/HTTP-MESSAGE

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

    PSR/HTTP-MESSAGE
  12. PSR/HTTP-MESSAGE Showtime

  13. PSR/HTTP-MESSAGE guzzlehttp/psr7 zendframework/zend-diactoros

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

  15. PSR/HTTP-MESSAGE symfony/psr-http-message-bridge uses: zendframework/zend-diactoros

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

  17. 2. guzzlehttp/guzzle

  18. GUZZLEHTTP/GUZZLE composer require guzzlehttp/guzzle

  19. GUZZLEHTTP/GUZZLE major version 6

  20. GUZZLEHTTP/GUZZLE composer.json ^6.1

  21. GUZZLEHTTP/GUZZLE composer.lock 6.1.0

  22. None
  23. GUZZLEHTTP/GUZZLE What’s new?

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

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

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

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

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

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

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

  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 - PROMISES "require": { "php": ">=5.6.0", "guzzlehttp/psr7": "^1.1", "guzzlehttp/promise":

    "^1.0" }
  37. GUZZLEHTTP/GUZZLE - PROMISES $promise = $client->sendAsync($request); $promise->then(function ($response) { $response->getBody();

    }); $promise->wait();
  38. GUZZLEHTTP/GUZZLE - PROMISES Demo 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 Previously (<4.0.0) symfony/event-dispatcher Later (>=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) { // Do before return $handler($request, $options) ->then(function (ResponseInterface $resp // Do after return $response; }); }; }
  43. GUZZLEHTTP/GUZZLE - MIDDLEWARES $stack = HandlerStack::create(); $client = new Client(['handler'

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

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

  46. GUZZLEHTTP/GUZZLE - WHAT ELSE What else?

  47. GUZZLEHTTP/GUZZLE - PROGRESS $request->getEmitter()->on( 'progress', function (ProgressEvent $event) { …

    } );
  48. GUZZLEHTTP/GUZZLE - PROGRESS Is gone! (>=5.0.0, <6.0.0)

  49. GUZZLEHTTP/GUZZLE - PROGRESS No progress on promise? Too bad!

  50. GUZZLEHTTP/GUZZLE - PROGRESS Roll your own

  51. GUZZLEHTTP/GUZZLE - PROGRESS // Psr\Http\Message\StreamInterface $stream = $response->getBody(); $stream->getSize(); $stream->eof();

    $stream->read(1024);
  52. GUZZLEHTTP/GUZZLE - PROGRESS use Symfony\Component\Console \Helper\ProgressBar;

  53. GUZZLEHTTP/GUZZLE - PROGRESS 10104/455642 [======>---------------------] 22% 14 sec/59 sec 2.4

    MB
  54. GUZZLEHTTP/GUZZLE - PROGRESS 15214/455642 [=========>------------------] 33% 21 sec/58 sec 2.6

    MB
  55. GUZZLEHTTP/GUZZLE - PROGRESS 20325/455642 [============>---------------] 43% 28 sec/59 sec 2.7

    MB
  56. GUZZLEHTTP/GUZZLE - PROGRESS 20325/455642 [===============>------------] 53% 35 sec/58 sec 2.7

    MB
  57. GUZZLEHTTP/GUZZLE - PROGRESS 20325/455642 [==================>---------] 59% 39 sec/58 sec 3.0

    MB
  58. GUZZLEHTTP/GUZZLE - PROGRESS 20325/455642 [==================>---------] 59% 39 sec/58 sec 3.0

    MB
  59. GUZZLEHTTP/GUZZLE - PROGRESS 20325/455642 [==================>---------] 59% 39 sec/58 sec 3.0

    MB
  60. GUZZLEHTTP/GUZZLE - PROGRESS 20325/455642 [==================>---------] 59% 39 sec/58 sec 3.0

    MB
  61. GUZZLEHTTP/GUZZLE - PROGRESS 20325/455642 [==================>---------] 59% 39 sec/58 sec 3.0

    MB
  62. GUZZLEHTTP/GUZZLE - PROGRESS 20325/455642 [==================>---------] 59% 39 sec/58 sec 3.0

    MB
  63. GUZZLEHTTP/GUZZLE - RECAP Uses PSR-7 interfaces Middlewares instead of Events

    Async & Promises
  64. 3. More PSR-7 usages

  65. Interface for PSR-7 HTTP Clients

  66. PHP-HTTP/HTTPLUG Imagine you’re writing an SDK for GitHub/Slack/Hipchat/DO/…

  67. PHP-HTTP/HTTPLUG composer require guzzlehttp/guzzle

  68. None
  69. PHP-HTTP/HTTPLUG - :CRY: “I already use Zend’s HTTP client! ”

  70. PHP-HTTP/HTTPLUG - :CRY: “I can’t install Guzzle v5 ánd v6!

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

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

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

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

  76. PHP-HTTP/HTTPLUG - ADAPTERS buzz-adapter react-adapter socket-adapter fopen-adapter file-get-contents-adapter native-curl-adapter pecl-adapter

  77. PHP-HTTP/HTTPLUG - COMPOSER.JSON "require": { "guzzlehttp/guzzle": "^5.0", "your-awesome/sdk": "^1.0",
 "php-http/guzzle5-adapter":

    "^0.1.0" }
  78. 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" }
  79. PHP-HTTP/HTTPLUG - COMPOSER.JSON $github = new Github\Client( // SDK new

    Http\Adapter\Guzzle6HttpAdapter( new GuzzleHttp\Client() ) );
  80. None
  81. PHP-HTTP/ADAPTER - DEMO Demo https:/ /github.com/hannesvdvreken/psr7-demo/tree/master/5-github

  82. ADAPTERS - HTTP - Logging - File system

  83. ADAPTERS Sadly not for caching

  84. ADAPTERS psr/cache PSR-6 being voted on

  85. Middlewares wrapping HTTP applications

  86. None
  87. MIDDLEWARE DEFINITION function ( RequestInterface $request, ResponseInterface $response, callable $next

    ) {…};
  88. MIDDLEWARE DEFINITION function (…) { // Before … $response =

    $next($request, $response); // After … return $response; }
  89. MIDDLEWARE DEFINITION Session Logging Authentication Robots.txt file HTTP Cache Firewall

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

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

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

  93. RECAP HTTP client interface Middleware definition + upgrade path

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

  95. Time for questions. @hannesvdvreken @phplimbe

  96. Thank you! @hannesvdvreken @phplimbe

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