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

HTTP-Clients in PHP - vom fopen-Wrapper zur Symfony-Komponente

Denis Brumann
September 26, 2019

HTTP-Clients in PHP - vom fopen-Wrapper zur Symfony-Komponente

HTTP-Requests aus einer PHP-Anwendung zu verschicken, war früher kompliziert.
Von aufwendigem Stream-Handling über eine Vielzahl an Bibliotheken hin zu einem gemeinsamen Standard hat sich viel getan. In meinem Talk gebe ich einen kurzen Einblick in die Historie von HTTP-Clients, einen Überblick über den PSR-18 Standard hin zu Symfonys HttpClient-Komponente und welche Vorteile sie bietet.

Denis Brumann

September 26, 2019
Tweet

More Decks by Denis Brumann

Other Decks in Programming

Transcript

  1. Vom fopen-Wrapper zum
    Symfony HttpClient

    View Slide

  2. Denis Brumann
    @dbrumann
    Software Developer
    Berlin, Germany

    View Slide

  3. 3
    HTTP-Requests in PHP
    eine kurze Geschichtsstunde
    By Copyright © 2005 David Monniaux - Own work, CC BY-SA 3.0, https://commons.wikimedia.org/w/index.php?curid=494543

    View Slide

  4. @dbrumann / [email protected] 4

    View Slide

  5. @dbrumann / [email protected] 5

    View Slide

  6. @dbrumann / [email protected] 6

    View Slide

  7. @dbrumann / [email protected] 7

    View Slide

  8. @dbrumann / [email protected] 8

    View Slide

  9. @dbrumann / [email protected] 9

    View Slide

  10. HTTP-Clients!

    View Slide

  11. @dbrumann / [email protected] 11
    composer require
    guzzle/guzzle

    View Slide

  12. @dbrumann / [email protected] 12

    View Slide

  13. @dbrumann / [email protected] 13

    View Slide

  14. @dbrumann / [email protected] 14
    composer require
    guzzlehttp/guzzle

    View Slide

  15. @dbrumann / [email protected] 15

    View Slide

  16. @dbrumann / [email protected] 16

    View Slide

  17. @dbrumann / [email protected] 17

    View Slide

  18. @dbrumann / [email protected] 18
    Library A Library B
    Guzzle 5
    Guzzle 6
    Your App

    View Slide

  19. @dbrumann / [email protected] 19

    View Slide

  20. View Slide

  21. @dbrumann / [email protected] 21

    View Slide

  22. @dbrumann / [email protected] 22
    Library A Library B
    Httplug
    Httplug
    Your App
    Httplug
    Guzzle 6-Adapter
    Guzzle 5-Adapter

    View Slide

  23. @dbrumann / [email protected] 23
    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)

    View Slide

  24. @dbrumann / [email protected] 24
    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.

    View Slide

  25. @dbrumann / [email protected] 25
    $request = new Request('GET', 'https://reqres.in/api/users');
    use GuzzleHttp\Psr7\Request;
    use Psr\Http\Client\ClientInterface;
    $response = $client->sendRequest($request);

    View Slide

  26. @dbrumann / [email protected] 26
    use Psr\Http\Client\ClientInterface;
    $response = $client->sendRequest($request);
    $request = $requestFactory->createRequest('GET',
    'https://reqres.in/api/users');
    use Psr\Http\Message\RequestFactoryInterface;

    View Slide

  27. @dbrumann / [email protected] 27

    View Slide

  28. @dbrumann / [email protected] 28
    HttpClientInterface::sendRequest()
    RequestInterface

    View Slide

  29. @dbrumann / [email protected] 29
    RequestExceptionInterface

    View Slide

  30. @dbrumann / [email protected] 30
    HttpClientInterface::sendRequest()
    RequestInterface
    ResponseInterface

    View Slide

  31. @dbrumann / [email protected] 31
    NetworkExceptionInterface

    View Slide

  32. @dbrumann / [email protected] 32
    A Client must not treat a well-formed HTTP
    request or HTTP response as an error condition.

    View Slide

  33. @dbrumann / [email protected] 33
    Wie konfiguriere ich meinen HttpClient?

    View Slide

  34. @dbrumann / [email protected] 34
    Wie kann ich Requests asynchron versenden?

    View Slide

  35. @dbrumann / [email protected] 35
    Was ist mit HTTP/2- oder HTTP/3-Features?

    View Slide

  36. @dbrumann / [email protected] 36

    View Slide

  37. @dbrumann / [email protected] 37
    composer require
    symfony/http-client

    View Slide

  38. @dbrumann / [email protected] 38

    View Slide

  39. @dbrumann / [email protected] 39
    $response = $this->httpClient->request(
    'GET',
    'https://reqres.in/api/users'
    );

    View Slide

  40. @dbrumann / [email protected] 40
    $response = $httpClient->request(
    'GET',
    'https://httpbin.org/get',
    [
    // these values are automatically encoded
    // before including them in the URL
    'query' => [
    'token' => '...',
    'name' => '...',
    ],
    ]
    );

    View Slide

  41. @dbrumann / [email protected] 41
    @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

    View Slide

  42. @dbrumann / [email protected] 43
    public function getHeaders(bool $throw = true): array;
    public function getContent(bool $throw = true): string;
    public function toArray(bool $throw = true): array;

    View Slide

  43. @dbrumann / [email protected] 44

    View Slide

  44. @dbrumann / [email protected] 45

    View Slide

  45. @dbrumann / [email protected] 46

    View Slide

  46. @dbrumann / [email protected] 47
    Library A Library B
    Guzzle 6
    Guzzle 5
    Your App

    View Slide

  47. @dbrumann / [email protected]bs.de 48
    Library A Library B
    Symfony 5
    Symfony 4
    Your App
    ?

    View Slide

  48. @dbrumann / [email protected] 49

    View Slide

  49. @dbrumann / [email protected] 50

    View Slide

  50. @dbrumann / [email protected] 51
    Symfony\Component\HttpClient\
    Psr18Client

    View Slide

  51. @dbrumann / [email protected] 52
    class="Symfony\Component\HttpClient\Psr18Client"
    >

    id="Psr\Http\Message\ResponseFactoryInterface"
    on-invalid="ignore"
    />
    id="Psr\Http\Message\StreamFactoryInterface"
    on-invalid="ignore"
    />

    View Slide

  52. @dbrumann / [email protected] 53
    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 .

    View Slide

  53. @dbrumann / [email protected] 54
    HttpClient::create()
    Curl
    Native

    View Slide

  54. @dbrumann / [email protected] 55

    View Slide

  55. @dbrumann / [email protected] 56

    View Slide

  56. @dbrumann 58
    Thank You!
    https://joind.in/talk/e8765

    View Slide