Symfony HttpClient

Symfony HttpClient

The Symfony HttpClient is quite new, but is built based on years of experience from other http clients. The talk explores how to do HTTP requests inside a PHP application from native functions to popular libraries and the PSR-18 standard leading to the component.

The slides have placeholders showing profiles in place of code samples from a demo project.


Denis Brumann

August 06, 2019


  1. Symfony HttpClient Denis Brumann @dbrumann Photo: Martina Nolte, Lizenz:

    Creative Commons by-sa-3.0 de
  2. None
  3. Denis Brumann @dbrumann Software Developer Berlin, Germany

  4. @dbrumann 4 Making a Request in PHP

  5. @dbrumann 5

  6. @dbrumann 6

  7. @dbrumann 7

  8. @dbrumann 8

  9. @dbrumann 9

  10. @dbrumann 10

  11. @dbrumann 11 Along Came Guzzle

  12. @dbrumann 12 composer require guzzle/guzzle

  13. @dbrumann 13

  14. @dbrumann 14

  15. @dbrumann 15

  16. @dbrumann 16

  17. @dbrumann 18 More Guzzle, More Cry

  18. @dbrumann 19 composer require guzzlehttp/guzzle

  19. @dbrumann 20

  20. @dbrumann 21

  21. @dbrumann 22 Library A Library B Guzzle 5 Guzzle 6

    Your App
  22. None
  23. @dbrumann 24

  24. @dbrumann 25

  25. @dbrumann 26 Library A Library B Httplug Httplug Your App

    Httplug Guzzle 6-Adapter Guzzle 5-Adapter
  26. @dbrumann 27 Encourage devs to depend on the HTTPlug interface

    instead of concrete clients. Provide good quality HTTP- related packages to the PHP community. Over time, make HTTPlug a PHP Standards Recommendation (PSR) Goals
  27. @dbrumann 29

  28. @dbrumann 30

  29. @dbrumann 31

  30. @dbrumann 32 The goal of this PSR is to allow

    developers to create libraries decoupled from HTTP client implementations. This will make libraries more reusable as it reduces the number of dependencies and lowers the likelihood of version conflicts.
  31. @dbrumann 33 HttpClientInterface::sendRequest() RequestInterface ResponseInterface

  32. @dbrumann 34 RequestExceptionInterface

  33. @dbrumann 36 NetworkExceptionInterface

  34. @dbrumann 37 A Client must not treat a well-formed HTTP

    request or HTTP response as an error condition.
  35. @dbrumann 38 How to create a RequestInterface?

  36. @dbrumann 36 $psr17Factory = new \Nyholm\Psr7\Factory\Psr17Factory(); $psr18Client = new \Buzz\Client\Curl($psr17Factory);

    $request = $psr17Factory->createRequest('GET', ''); $response = $psr18Client->sendRequest($request);
  37. @dbrumann 39 $psr17Factory = new \Nyholm\Psr7\Factory\Psr17Factory(); $psr18Client = new \Buzz\Client\Curl($psr17Factory);

    $request = $psr17Factory->createRequest('GET', ''); $response = $psr18Client->sendRequest($request);
  38. @dbrumann 37 How to configure the client?

  39. @dbrumann 38 How to perform batch requests?

  40. @dbrumann 39 How to perform asynchronous requests?

  41. @dbrumann 43

  42. @dbrumann 44 Library A Library B Guzzle 5 Guzzle 6

    Your App
  43. @dbrumann 45

  44. @dbrumann 46

  45. @dbrumann 47

  46. @dbrumann 48 $response = $this->httpClient->request( 'GET', '' ); $data =

  47. @dbrumann 49 composer require symfony/http-client

  48. @dbrumann 50

  49. @dbrumann 51 HttpClient::create() Curl Native

  50. @dbrumann 52 Symfony\Component\HttpClient\Psr18Client

  51. @dbrumann 53

  52. @dbrumann 54 <service id="psr18.http_client" class="Symfony\Component\HttpClient\Psr18Client" > <argument type="service" id="http_client" />

    <argument type="service" id="Psr\Http\Message\ResponseFactoryInterface" on-invalid="ignore" /> <argument type="service" id="Psr\Http\Message\StreamFactoryInterface" on-invalid="ignore" /> </service>
  53. @dbrumann 55 $response = $httpClient->request( 'GET', '', [ // these

    values are automatically encoded // before including them in the URL 'query' => [ 'token' => '...', 'name' => '...', ], ] );
  54. @dbrumann 56

  55. None
  56. @dbrumann 58 @throws TransportExceptionInterface When a network error occurs @throws

    RedirectionExceptionInterface On a 3xx when $throw is true and the "max_redirects" option has been reached @throws ClientExceptionInterface On a 4xx when $throw is true @throws ServerExceptionInterface On a 5xx when $throw is true
  57. @dbrumann 60 public function getHeaders(bool $throw = true): array; public

    function getContent(bool $throw = true): string; public function toArray(bool $throw = true): array;
  58. @dbrumann 61

  59. @dbrumann 60 T h e c o m p o

    n e n t i s b u i l t f o r m a x i m u m H T T P p e r f o r m a n c e . B y d e s i g n , i t i s c o m p a t i b l e w i t h H T T P / 2 a n d w i t h d o i n g c o n c u r r e n t a s y n c h r o n o u s s t r e a m e d a n d m u l t i p l e x e d r e q u e s t s / r e s p o n s e s . E v e n w h e n d o i n g re g u l a r s y n c h ro n o u s c a l l s , t h i s d e s i g n a l l o w s k e e p i n g c o n n e c t i o n s t o re m o t e h o s t s o p e n b e t w e e n re q u e s t s , i m p ro v i n g p e r f o r m a n c e b y s a v i n g re p e t i t i v e D N S re s o l u t i o n , S S L n e g o t i a t i o n , e t c . To l e v e r a g e a l l t h e s e d e s i g n b e n e f i t s , t h e c U R L e x t e n s i o n i s n e e d e d .
  60. @dbrumann 61 Thank You!