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

Hannes Van De Vreken

January 31, 2016
Tweet

More Decks by Hannes Van De Vreken

Other Decks in Technology

Transcript

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

    $promise->then(function (Res $response) { $response->getBody(); }); $promise->wait();
  2. 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; }); }; }
  3. GUZZLEHTTP/GUZZLE - MIDDLEWARES $stack = HandlerStack::create(); $client = new Client(['handler'

    => $stack]); $stack->push($middleware); $stack->unshift($middleware); $stack->remove($middleware);
  4. 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
  5. 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" }
  6. MIDDLEWARE DEFINITION function ($request, $response, $next) { // Alter request,

    if you want $response = $next($request, $response); // Alter response, if you want return $response; }
  7. • 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