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

Modern HTTP handling with PHP - Breda PHP

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

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/event/bredaphp-march-2016/psr-7-http-messages-in-the-wild

Hannes Van De Vreken

March 31, 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
    @bredaphp

    View Slide

  2. Hi, my name is Hannes.

    View Slide

  3. !

    View Slide

  4. madewithlove.be

    View Slide

  5. PSR-7

    View Slide

  6. PSR-7
    Common interfaces for HTTP messages

    View Slide

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

    View Slide

  8. 1. psr/http-message

    View Slide

  9. Common interfaces for HTTP messages

    View Slide

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

    View Slide

  11. Common interfaces for HTTP messages

    View Slide

  12. Pre-PSR-7

    View Slide

  13. composer req symfony/http-foundation

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  20. 2. guzzlehttp/guzzle

    View Slide

  21. GUZZLEHTTP/GUZZLE
    composer req guzzlehttp/guzzle

    View Slide

  22. GUZZLEHTTP/GUZZLE
    composer.json
    ^6.1

    View Slide


  23. View Slide

  24. semver.mwl.be

    View Slide

  25. GUZZLEHTTP/GUZZLE
    What’s new?

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  29. View Slide

  30. View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  35. GUZZLEHTTP/GUZZLE - PROMISES
    Promises

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  50. 3. More PSR-7 usages

    View Slide

  51. Decoupling HTTP Clients

    View Slide

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

    View Slide

  53. composer req guzzlehttp/guzzle

    View Slide

  54. View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  58. View Slide

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

    View Slide

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

    View Slide

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

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

    View Slide

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

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

    View Slide

  63. 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 Slide

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

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

    View Slide

  65. 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 Slide

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

    View Slide

  67. View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  72. Middlewares
    wrapping HTTP applications

    View Slide

  73. View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  79. MIGRATION FROM STACKPHP
    stack middleware

    psr-7 middleware

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  83. Time for questions.
    @hannesvdvreken
    @bredaphp

    View Slide

  84. Thank you!
    https://joind.in/talk/e833b
    @hannesvdvreken
    @bredaphp

    View Slide

  85. • 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 Slide