Modern HTTP handling with PHP - Meetup PHP Leuven

Modern HTTP handling with PHP - Meetup PHP Leuven

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. Hannes Van De Vreken (madewithlove) will talk about the impact of PSR-7 on the way frameworks implement these messages, and give a few live examples.

39eb3f3d313b13f05534e496285040b8?s=128

Hannes Van De Vreken

September 03, 2015
Tweet

Transcript

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

    @hannesvdvreken
  2. Hi, my name is Hannes.

  3. madewithlove

  4. None
  5. Highlighted packages

  6. 1. psr/http-message 2. guzzlehttp/guzzle 3. php-http/adapter

  7. 1. psr/http-message

  8. PSR/HTTP-MESSAGE interfaces

  9. PSR/HTTP-MESSAGE MessageInterface > RequestInterface > ResponseInterface UriInterface StreamInterface (body)

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

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

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

    PSR/HTTP-MESSAGE
  13. PSR/HTTP-MESSAGE https:/ /github.com/php-fig/http-message/tree/master/src

  14. 2. guzzlehttp/guzzle

  15. GUZZLEHTTP/GUZZLE composer require guzzlehttp/guzzle

  16. GUZZLEHTTP/GUZZLE composer.json ^6.0

  17. GUZZLEHTTP/GUZZLE composer.lock 6.0.2

  18. GUZZLEHTTP/GUZZLE semver.mwl.be

  19. None
  20. GUZZLEHTTP/GUZZLE What’s new?

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

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

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

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

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

  26. GUZZLEHTTP/GUZZLE - PSR-7 "require": { "php": ">=5.5.0", "guzzlehttp/psr7": "~1.1", }

  27. GUZZLEHTTP/GUZZLE - PSR-7 Factories to convert $psrRequest ⁶ $symfonyRequest $psrResponse

    ⁶ $symfonyResponse
  28. GUZZLEHTTP/GUZZLE - PSR-7 Demo https:/ /github.com/hannesvdvreken/psr7-demo/tree/master/1-guzzle

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

  30. GUZZLEHTTP/GUZZLE - PROMISES Promises

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

  32. GUZZLEHTTP/GUZZLE - PROMISES "require": { "php": ">=5.5.0", "guzzlehttp/psr7": "~1.1", "guzzlehttp/promise":

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

    }); $promise->wait();
  34. GUZZLEHTTP/GUZZLE - PROMISES Demo https:/ /github.com/hannesvdvreken/psr7-demo/tree/master/2-promise https:/ /github.com/hannesvdvreken/psr7-demo/tree/master/3-multi

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

  36. GUZZLEHTTP/GUZZLE - EVENTING Previously (<4.0.0) symfony/event-dispatcher Later (>=4.0.0, <6.0.0) GuzzleHttp\Event\EmitterInterface

    GuzzleHttp\Event\SubscriberInterface
  37. GUZZLEHTTP/GUZZLE - MIDDLEWARES Now (>=6.0.0) Callable middlewares

  38. GUZZLEHTTP/GUZZLE - MIDDLEWARES $stack = HandlerStack::create(); $client = new Client(['handler'

    => $stack]); $stack->push($middleware); $stack->unshift($middleware); $stack->remove($middleware);
  39. GUZZLEHTTP/GUZZLE - MIDDLEWARES $middleware = function ($handler) { return function

    ($request, $options) use ($handler) { // Do before return $handler($request, $options) ->then(function () { // Do after }); }; }
  40. GUZZLEHTTP/GUZZLE - MIDDLEWARES $middleware = GuzzleHttp\Middleware::log( $psrLogger, new GuzzleHttp\MessageFormatter() );

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

  42. GUZZLEHTTP/GUZZLE - WHAT ELSE What else?

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

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

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

  46. GUZZLEHTTP/GUZZLE - PROGRESS use Symfony\Component\Console \Helper\ProgressBar;

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

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

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

    MB
  50. GUZZLEHTTP/GUZZLE - PROGRESS Roll your own

  51. GUZZLEHTTP/GUZZLE - PROGRESS $response = $client->get($uri); // Psr\Http\Message\StreamInterface $stream =

    $response->getBody(); $stream->getSize(); $stream->eof(); $stream->read(1024);
  52. 3. php-http/adapter

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

  54. PHP-HTTP/ADAPTER composer require guzzlehttp/guzzle

  55. None
  56. PHP-HTTP/ADAPTER - :CRY: “I already use Zend’s HTTP client! ”

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

  58. PHP-HTTP/ADAPTER - SOLUTION Decouple from the HTTP client

  59. None
  60. PHP-HTTP/ADAPTER - SOLUTION $adapter = new Adapter($guzzleClient); $psrRes = $adapter->sendRequest($psrReq);

    $client = new Client($adapter); $psrRes = $client->get($url);
  61. PHP-HTTP/ADAPTER - SOLUTION public function __construct( Http\Adapter\HttpAdapter $adapter ) {

    $this->adapter = $adapter; } public function __construct( Http\Client\HttpClient $client ) { $this->client = $client; }
  62. PHP-HTTP/ADAPTER - SOLUTION "require": { "php-http/adapter": "^0.1.0", "php-http/adapter-client": "^0.1.0",
 "php-http/adapter-implementation":

    "^0.1.0" }
  63. PHP-HTTP/ADAPTER - SOLUTION Virtual package "provide": {
 "php-http/adapter-implementation": "^0.1.0" }

  64. None
  65. PHP-HTTP/ADAPTER - ADAPTERS guzzle6-adapter guzzle5-adapter guzzle4-adapter guzzle3-adapter zend2-adapter zend1-adapter cake-adapter

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

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

    "^0.1.0" }
  68. PHP-HTTP/ADAPTER - 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" }
  69. None
  70. PHP-HTTP/ADAPTER - DEMO Demo https:/ /github.com/hannesvdvreken/psr7-demo/tree/master/5-github

  71. RECAP 1. psr/http-message 2. guzzlehttp/guzzle 3. php-http/adapter

  72. Time for questions. @hannesvdvreken

  73. Thank you! @hannesvdvreken

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