Learning the PHP-FIG HTTP Stack (Midwest PHP 2019)

Learning the PHP-FIG HTTP Stack (Midwest PHP 2019)

The PHP-FIG has produced four PSRs relating to HTTP: PSR-7 HTTP message interfaces, PSR-15 HTTP Server Request Handlers, PSR-17 HTTP Factories and PSR-18 HTTP Client. Learn the differences between these PSRs and how they work together to enable a rich set of tools making it easier for applications and libraries to work with HTTP in PHP.

23d971deeb3975a7d28246192fbbe7b7?s=128

Beau Simensen

March 08, 2019
Tweet

Transcript

  1. 1.

    Learning the PHP-FIG HTTP Stack Beau Simensen Technical Strategy Consultant

    beausimensen.com • astrocasts.com • thatpodcast.io
  2. 2.
  3. 3.
  4. 4.

    $client = create_http_client(); // implementation specific $response = $client ->request('GET',

    'http: // www.php.net'); if ($response ->getStatusCode() == 200) { $content = $response ->getContent(); } $response = $client ->request('GET', 'http: //api/returning.json'); if ($response ->getContentType() == 'application/json') { $json = json_decode($response ->getContent()); }
  5. 5.

    interface ClientInterface { /** * @return ResponseInterface */ public function

    request($method, $url, array $headers = [], $content = null); }
  6. 6.

    –Kris Wallsmith, 2012-03-24 "[...] I think we should take a

    step back and start with interfaces for an HTTP request and an HTTP response. Once those are done, then we can move on to an HTTP client."
  7. 8.

    –Beau Simensen, 2012-06-17 "Either the client needs to construct the

    request itself (factory) or we need to provide a PSR request implementation that anyone can use."
  8. 9.

    interface ClientInterface { /** * @return RequestInterface */ public function

    createRequest($method, $url, array $headers = [], $content = null); }
  9. 11.

    –Evert Pot, 2012-03-24 "If you're taking this approach, you will

    find that you probably forgot a lot of HTTP features :)"
  10. 12.

    –Evert Pot, 2012-03-24 "If you want to keep things simple,

    I'd strongly suggest focusing on the following..." - http method - url - http version - headers - stream for body - http status code - human-readable status - http version - headers - stream for body Request Response
  11. 14.
  12. 16.
  13. 17.
  14. 18.
  15. 19.
  16. 20.

    namespace Symfony\Component\HttpFoundation; class Request { public function __construct( array $query

    = [], array $request = [], array $attributes = [], array $cookies = [], array $files = [], array $server = [], $content = null ) { // ... } }
  17. 21.

    namespace Symfony\Component\HttpFoundation; class Request { public static function createFromGlobals() {

    return self ::createRequestFromFactory( $_GET, $_POST, [], $_COOKIE, $_FILES, $_SERVER ); } }
  18. 22.

    namespace Zend\Http\PhpEnvironment; class Request extends \Zend\Http\Request { public function __construct()

    { $this ->setEnv(new Parameters($_ENV)); if ($_GET) { $this ->setQuery(new Parameters($_GET)); } if ($_POST) { $this ->setPost(new Parameters($_POST)); } if ($_COOKIE) { $this ->setCookies(new Parameters($_COOKIE)); } if ($_FILES) { $files = $this ->mapPhpFiles(); $this ->setFiles(new Parameters($files)); } $this ->setServer(new Parameters($_SERVER)); } }
  19. 23.
  20. 24.
  21. 25.
  22. 26.
  23. 28.
  24. 30.
  25. 37.
  26. 38.
  27. 41.
  28. 42.

    class ApiTheRightWay { private $client; public function __construct($client) { $this

    ->client = $client; } public function getStatus() { $request = new GuzzleHttp\Psr7\Request(); $response = $this ->client ->send($request); return json_decode($response ->getBody() ->getContents(), true); } }
  29. 43.

    interface DoublePassHttpMiddleware { public function __invoke( RequestInterface $request, ResponseInterface $response,

    callable $next ): ResponseInterface; } interface SinglePassHttpMiddleware { public function __invoke( RequestInterface $request, callable $next ): ResponseInterface; }
  30. 44.

    interface DoublePassHttpMiddleware { public function __invoke( RequestInterface $request, ResponseInterface $response,

    callable $next ): ResponseInterface; } interface SinglePassHttpMiddleware { public function __invoke( RequestInterface $request, callable $next ): ResponseInterface; }
  31. 47.
  32. 48.
  33. 51.

    –Kris Wallsmith, 2012-03-24 "[...] I think we should take a

    step back and start with interfaces for an HTTP request and an HTTP response. Once those are done, then we can move on to an HTTP client."
  34. 53.

    interface DoublePassHttpMiddleware { public function __invoke( RequestInterface $request, ResponseInterface $response,

    callable $next ): ResponseInterface; } interface SinglePassHttpMiddleware { public function __invoke( RequestInterface $request, callable $next ): ResponseInterface; }
  35. 54.

    class ChaosMiddleware implements SinglePassHttpMiddleware { public function __invoke( RequestInterface $request,

    callable $next ): ResponseInterface { if (rand(0, 99) === 0) { return (new GuzzleHttp\Psr7\Response(404)); } return $next($request); } }
  36. 55.

    class ChaosMiddleware implements DoublePassHttpMiddleware { public function __invoke( RequestInterface $request,

    ResponseInterface $response, callable $next ): ResponseInterface { if (rand(0, 99) === 0) { return $response ->withStatus(404); } return $next($request); } }
  37. 56.

    interface DoublePassHttpMiddleware { public function __invoke( RequestInterface $request, ResponseInterface $response,

    callable $next ): ResponseInterface; } interface SinglePassHttpMiddleware { public function __invoke( RequestInterface $request, callable $next ): ResponseInterface; }
  38. 57.

    interface DoublePassHttpMiddleware { public function __invoke( RequestInterface $request, ResponseInterface $response,

    callable $next ): ResponseInterface; } interface SinglePassHttpMiddleware { public function __invoke( RequestInterface $request, callable $next ): ResponseInterface; }
  39. 61.

    –My personal soapbox for many, many years... “We either pass

    the object or we introduce factories.”
  40. 63.

    interface DoublePassHttpMiddleware { public function __invoke( RequestInterface $request, ResponseInterface $response,

    callable $next ): ResponseInterface; } interface SinglePassHttpMiddleware { public function __invoke( RequestInterface $request, callable $next ): ResponseInterface; }
  41. 64.

    namespace Psr\Http\Server; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; interface MiddlewareInterface { public

    function process( ServerRequestInterface $request, RequestHandlerInterface $handler ): ResponseInterface; }
  42. 66.
  43. 67.
  44. 68.
  45. 69.
  46. 70.
  47. 71.
  48. 72.
  49. 73.
  50. 74.
  51. 75.
  52. 76.
  53. 77.
  54. 81.

    namespace Psr\Http\Message; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\UriInterface; interface ServerRequestFactoryInterface { public

    function createServerRequest( string $method, $uri, array $serverParams = [] ): ServerRequestInterface; }
  55. 82.

    namespace Psr\Http\Message; use Psr\Http\Message\StreamInterface; interface StreamFactoryInterface { public function createStream(string

    $content = ''): StreamInterface; public function createStreamFromFile( string $filename, string $mode = 'r' ): StreamInterface; public function createStreamFromResource($resource): StreamInterface; }
  56. 83.

    namespace Psr\Http\Message; use Psr\Http\Message\StreamInterface; use Psr\Http\Message\UploadedFileInterface; interface UploadedFileFactoryInterface { public

    function createUploadedFile( StreamInterface $stream, int $size = null, int $error = \UPLOAD_ERR_OK, string $clientFilename = null, string $clientMediaType = null ): UploadedFileInterface; }
  57. 85.

    class ApiTheRightWay { private $client; public function __construct($client) { $this

    ->client = $client; } public function getStatus() { $request = new GuzzleHttp\Psr7\Request(); $response = $this ->client ->send($request); return json_decode($response ->getBody() ->getContents(), true); } }
  58. 86.

    class ApiTheRightWay { private $client; private $requestFactory; public function __construct($client,

    RequestFactoryInterface $requestFactory) { $this ->client = $client; $this ->requestFactory = $requestFactory; } public function getStatus() { $request = $this ->requestFactory ->createRequest(); $response = $this ->client ->send($request); return json_decode($response ->getBody() ->getContents(), true); } }
  59. 87.

    class ChaosMiddleware implements SinglePassHttpMiddleware { public function __invoke( RequestInterface $request,

    callable $next ): ResponseInterface { if (rand(0, 99) === 0) { return (new GuzzleHttp\Psr7\Response(404)); } return $next($request); } }
  60. 88.

    class ChaosMiddleware implements SinglePassHttpMiddleware { private $responseFactory; public function __construct(ResponseFactoryInterface

    $responseFactory) { $this ->responseFactory = $responseFactory; } public function __invoke( RequestInterface $request, callable $next ): ResponseInterface { if (rand(0, 99) === 0) { return $this ->responseFactory ->createResponse(404); } return $next($request); } }
  61. 89.
  62. 90.
  63. 91.
  64. 92.
  65. 96.

    class ApiTheRightWay { private $client; private $requestFactory; public function __construct($client,

    RequestFactoryInterface $requestFactory) { $this ->client = $client; $this ->requestFactory = $requestFactory; } public function getStatus() { $request = $this ->requestFactory ->createRequest(); $response = $this ->client ->send($request); return json_decode($response ->getBody() ->getContents(), true); } }
  66. 97.

    class ApiTheRightWay { private $client; private $requestFactory; public function __construct(

    ClientInterface $client, RequestFactoryInterface $requestFactory ) { $this ->client = $client; $this ->requestFactory = $requestFactory; } public function getStatus() { $request = $this ->requestFactory ->createRequest(); $response = $this ->client ->sendRequest($request); return json_decode($response ->getBody() ->getContents(), true); } }
  67. 98.
  68. 99.
  69. 100.

    Learning the PHP-FIG HTTP Stack Beau Simensen Technical Strategy Consultant

    beausimensen.com • astrocasts.com • thatpodcast.io Thanks!