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

Modern HTTP handling with PHP - Dutch PHP conference

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

Feedback here: https://joind.in/talk/1786a

Hannes Van De Vreken

June 25, 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
    @dpcon #dpc16

    View full-size slide

  2. Hi, my name is Hannes.

    View full-size slide

  3. madewithlove.be
    20 ×
    !%&'()

    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.2

    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 - WHAT’S NEW
    What else?

    View full-size slide

  28. GUZZLEHTTP/GUZZLE - PROMISES
    Promises

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  31. 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

  32. GUZZLEHTTP/GUZZLE
    Demo
    https:/
    /github.com/hannesvdvreken/psr7-demo/tree/master/1-guzzle
    https:/
    /github.com/hannesvdvreken/psr7-demo/tree/master/2-promise
    https:/
    /github.com/hannesvdvreken/psr7-demo/tree/master/3-multi

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  36. 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

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  43. 3. More PSR-7 usages

    View full-size slide

  44. Decoupling HTTP Clients

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

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

    View full-size slide

  48. 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

  49. 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

  50. 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

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  54. 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

  55. Middlewares
    wrapping HTTP applications

    View full-size slide

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

    View full-size slide

  57. 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

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

    View full-size slide

  59. MIDDLEWARE DEFINITION
    Build your stack with:
    relay/relay
    mindplay/middleman

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  62. MIGRATION FROM STACKPHP
    stack middleware

    psr-7 middleware

    View full-size slide

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

    View full-size slide

  64. HTTP MIDDLEWARES
    PSR-15
    will change that

    View full-size slide

  65. HTTP MIDDLEWARES
    PSR-15
    is not accepted yet

    View full-size slide

  66. HTTP MIDDLEWARES - PSR-15
    MiddlewareInterface
    > ClientMiddlewareInterface
    > ServerMiddlewareInterface
    FrameInterface
    StackInterface

    View full-size slide

  67. HTTP MIDDLEWARES
    https:/
    /github.com/php-fig/fig-standards/
    blob/master/proposed/http-middleware/
    middleware.md

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  70. Thank you!
    https://joind.in/talk/1786a
    @hannesvdvreken
    @dpcon #dpc16

    View full-size slide

  71. Time for questions.
    @hannesvdvreken
    @dpcon #dpc16

    View full-size slide

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