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

Modern HTTP handling with PHP - PHP UK Conference

Modern HTTP handling with PHP - PHP UK 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.

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/b34d2

Hannes Van De Vreken

February 18, 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
    @phpukconference

    View full-size slide

  2. Hi, my name is Hannes.

    View full-size slide

  3. madewithlove.be

    View full-size slide

  4. PSR-7
    Common interfaces for HTTP messages

    View full-size slide

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

    View full-size slide

  6. 1. psr/http-message

    View full-size slide

  7. Common interfaces for HTTP messages

    View full-size slide

  8. Common interfaces for HTTP messages
    - Works in any Framework
    - Both for HTTP Clients and HTTP applications
    - HTTP version independent

    View full-size slide

  9. Common interfaces for HTTP messages

    View full-size slide

  10. composer req symfony/http-foundation

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  13. $newRequest = $request
    ->withHeader($name, $value);
    var_dump($newRequest === $request);
    > false
    PSR/HTTP-MESSAGE

    View full-size slide

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

    View full-size slide

  15. PSR/HTTP-MESSAGE - RECAP
    MessageInterface
    RequestInterface
    ResponseInterface
    StreamInterface❗
    UriInterface
    ServerRequestInterface
    ($_GET, $_POST, $_SERVER, $_COOKIES, $_FILES)

    View full-size slide

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

    View full-size slide

  17. 2. guzzlehttp/guzzle

    View full-size slide

  18. GUZZLEHTTP/GUZZLE
    composer req guzzlehttp/guzzle

    View full-size slide

  19. GUZZLEHTTP/GUZZLE
    composer.json
    ^6.1

    View full-size slide

  20. semver.mwl.be

    View full-size slide

  21. GUZZLEHTTP/GUZZLE
    What’s new?

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  25. GUZZLEHTTP/GUZZLE - PSR-7
    use GuzzleHttp\Psr7\Request;
    $psrRequest = new Request(
    'GET', $uri, $headers, $body
    );

    View full-size slide

  26. GUZZLEHTTP/GUZZLE - PSR-7
    $client = new GuzzleHttp\Client();
    $client->send($psrRequest);

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  29. GUZZLEHTTP/GUZZLE - PROMISES
    Promises

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  32. GUZZLEHTTP/GUZZLE - PROMISES
    use Psr\Http\Message\ResponseInterface as Res;
    $promise = $client->sendAsync($request);
    $promise->then(function (Res $response) {
    $response->getBody();
    });
    $promise->wait();

    View full-size slide

  33. GUZZLEHTTP/GUZZLE - PROMISES
    Demo
    https:/
    /github.com/hannesvdvreken/psr7-demo/tree/master/2-promise
    https:/
    /github.com/hannesvdvreken/psr7-demo/tree/master/3-multi

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  37. 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;
    });
    };
    }

    View full-size slide

  38. GUZZLEHTTP/GUZZLE - MIDDLEWARES
    use GuzzleHttp\Middleware;
    $middleware = Middleware::mapRequest(
    function (RequestInterface $request) {
    // Modify request
    return $request;
    }
    );

    View full-size slide

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

    View full-size slide

  40. GUZZLEHTTP/GUZZLE - MIDDLEWARES
    $middleware = GuzzleHttp\Middleware::log(
    $psr3Logger,
    new GuzzleHttp\MessageFormatter()
    );

    View full-size slide

  41. GUZZLEHTTP/GUZZLE - MIDDLEWARES
    $stack = HandlerStack::create();
    $client = new Client(['handler' => $stack]);
    $stack->push($middleware);
    $stack->unshift($middleware);
    $stack->remove($middleware);

    View full-size slide

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

    View full-size slide

  43. GUZZLEHTTP/GUZZLE - RECAP
    Provides PSR-7 interfaced objects
    Middlewares instead of Eventing
    Async & Promises

    View full-size slide

  44. 3. More PSR-7 usages

    View full-size slide

  45. Decoupling HTTP Clients

    View full-size slide

  46. Imagine you’re writing an SDK
    for GitHub/Slack/Hipchat/DO/…

    View full-size slide

  47. composer req guzzlehttp/guzzle

    View full-size slide

  48. “I can’t install Guzzle v5 ánd v6!

    View full-size slide

  49. “I already use Zend’s HTTP client!

    View full-size slide

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

    View full-size slide

  51. PHP-HTTP/HTTPLUG - SOLUTION
    interface HttpAdapter
    {
    public function sendRequest($request);
    public function sendRequests(array $requests);
    }

    View full-size slide

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

    View full-size slide

  53. PHP-HTTP/HTTPLUG - SOLUTION
    "require": {
    …

    "php-http/client-implementation": "^0.1.0",
    }

    View full-size slide

  54. PHP-HTTP/HTTPLUG - SOLUTION
    Virtual package
    "provide": {

    "php-http/client-implementation": "^0.1.0",
    }

    View full-size slide

  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

    View full-size slide

  56. PHP-HTTP/HTTPLUG - COMPOSER.JSON
    "require": {
    "guzzlehttp/guzzle": "^5.0",
    "your-awesome/sdk": "^1.0",

    "php-http/guzzle5-adapter": "^0.1.0"
    }

    View full-size slide

  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"
    }

    View full-size slide

  58. PHP-HTTP/HTTPLUG - WRAPPING
    $github = new Your\Sdk\Client(
    new Http\Adapter\Guzzle6HttpAdapter(
    new GuzzleHttp\Client()
    )
    );

    View full-size slide

  59. START USING PSR-7 IN YOUR APP
    What about existing Symfony
    Request/Response objects?

    View full-size slide

  60. START USING PSR-7 IN YOUR APP
    symfony/psr-http-message-bridge

    View full-size slide

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

    View full-size slide

  62. START USING PSR-7 IN YOUR APP
    Symfony request/response object
    ⁷ ⁷
    Zend request/response object
    \Psr\Http\Message\RequestInterface
    type hinted

    View full-size slide

  63. Middlewares
    wrapping HTTP applications

    View full-size slide

  64. MIDDLEWARE DEFINITION
    function (
    RequestInterface $request,
    ResponseInterface $response,
    callable $next
    ) {…};

    View full-size slide

  65. MIDDLEWARE DEFINITION
    function ($request, $response, $next) {
    // Alter request, if you want
    $response = $next($request, $response);
    // Alter response, if you want
    return $response;
    }

    View full-size slide

  66. MIDDLEWARE DEFINITION
    Session
    Logging
    Authentication
    Robots.txt file
    HTTP Cache
    Firewall
    Cors
    Throttling

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  69. MIGRATION FROM STACKPHP
    stack middleware

    psr-7 middleware

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  72. Time for questions.
    @hannesvdvreken
    @phpukconference

    View full-size slide

  73. Thank you!
    https://joind.in/talk/b34d2
    @hannesvdvreken
    @phpukconference

    View full-size slide

  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

    View full-size slide