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

Guzzle Makes HTTP Not Hard

Guzzle Makes HTTP Not Hard

Guzzle is both an HTTP client and a framework for building web service clients. It gives you all the power of cURL, but is really easy to use. With a sleek, event-driven request system and the ability to make requests in parallel, Guzzle should be a permanent part of your PHP tool belt.

Jeremy Lindblom

September 18, 2014
Tweet

More Decks by Jeremy Lindblom

Other Decks in Programming

Transcript

  1. Guzzle PHP!
    Makes HTTP Easy!
    by @jeremeamia • for @NomadPHP!

    View Slide

  2. Hi, I’m Jeremy.!
    @jeremeamia!
    Seattle PHP User Group!
    @seaphp!
    AWS SDK for PHP!
    @awsforphp!

    View Slide

  3. AWS SDK for PHP
    !
    !
    Built on Guzzle!!

    View Slide

  4. Guzzle!
    !
    Thanks to!
    @mtdowling!
    for Guzzle!!
    #GuzzlePHP!
    !

    View Slide

  5. HTTP is Kinda Hard!
    •  URLs!
    •  Hosts!
    •  Headers!
    •  Query strings!
    •  GET/POST/PUT/etc.!
    •  Status codes!
    •  cURL!
    •  Proxies!
    •  Message Integrity!
    •  HTTPS!
    •  Post bodies!
    •  Ports!
    •  REST!
    •  Cookies!
    •  Hypermedia!
    •  Streams!
    •  RFCs!
    •  Caching!

    View Slide

  6. Guzzle!
    is…!

    View Slide

  7. …an open source PHP library!

    View Slide

  8. …a well-crafted PHP library!
    •  Follows good OOP patterns and practices!
    •  Follows PSRs 1–4!
    •  Uses good tools: Composer, Travis CI,
    PHPUnit, GitHub, make, sphinx!
    •  High test coverage!
    •  Good documentation!
    •  Extensible via DI, callbacks, and events!

    View Slide

  9. …an HTTP client!
    e.g., https://api.github.com!

    View Slide

  10. …an HTTP client!
    $client  =  new  GuzzleHttp\Client([  
           'base_url'  =>  'https://api.github.com'  
    ]);  
    $response  =  $client-­‐>get('user',  [  
           'headers'  =>  [  
                   'Accept'  =>  'application/json'  
           ]  
    ]);  
    echo  $response-­‐>getStatusCode();  
    print_r($response-­‐>json());  

    View Slide

  11. …an HTTP client!
    $r  =  $client-­‐>METHOD('PATH',  [PARAMS]);  
     

    View Slide

  12. …an HTTP client!
    $r  =  $client-­‐>METHOD('PATH',  [PARAMS]);  
    $r  =  $client-­‐>get('PATH',  [PARAMS]);  
    $r  =  $client-­‐>post('PATH',  [PARAMS]);  
    $r  =  $client-­‐>put('PATH',  [PARAMS]);  
    $r  =  $client-­‐>delete('PATH',  [PARAMS]);  
    $r  =  $client-­‐>head('PATH',  [PARAMS]);  
    $r  =  $client-­‐>options('PATH',  [PARAM]);  
     

    View Slide

  13. …an HTTP client!
    $r  =  $client-­‐>METHOD('PATH',  [  
           'headers'  =>  […],  
           'body'        =>  […],  
           'query'      =>  […],  
           'cookies'  =>  […],  
           'timeout'  =>  […],  
           'proxy'      =>  […],  
       //  …  
    ]);  
     

    View Slide

  14. …an HTTP client!
    $r  =  $client-­‐>METHOD('PATH',  [PARAMS]);  
     

    View Slide

  15. …packed with features!
    •  Pluggable HTTP adapters!
    •  Can send requests serially or in parallel!
    •  Doesn't require cURL, but uses it by default!
    •  Streams data for both uploads & downloads!
    •  Provides event hooks & plugins for cookies,
    caching, logging, OAuth, mocks, etc.!
    •  Keep-Alive & connection pooling!
    •  SSL Verification & Connection timeouts!

    View Slide

  16. …a web service client framework!
    $s3  =  Aws\S3\S3Client::factory();  
    $result  =  $s3-­‐>listObjects([  
           'Bucket'  =>  $bucket  
    ]);  
    foreach  ($result['Contents']  as  $obj)  {  
           echo  $obj['Key'];  
    );  

    View Slide

  17. …popular!
    •  Over 3900 stars on GitHub!
    •  Used in many projects including!
    – Drupal 8!
    – Goutte!
    – AWS SDK for PHP!
    – Laravel Mail Component!
    – Tumblr Client!
    – Nearly 1000 other Packagist packages!

    View Slide

  18. And now!
    a story about
    Guzzle by!
    @phpbard!

    View Slide

  19. This is a story about an elePHPant named Guzzle.!
    She consumes lots of data. But how? It's no puzzle.!

    View Slide

  20. Armed with HTTP, she knows how to transport!
    Text and files of any size and sort.!

    View Slide

  21. From client to server, and from server to client,!
    Her requests and responses are RFC compliant.!

    View Slide

  22. And beyond the basics of just cookie and header.!
    There's much more to Guzzle that just makes her better:!

    View Slide

  23. From parallel requests, from events and plugins,!
    To web service descriptions. And this just begins…!

    View Slide

  24. To show you all that Guzzle can do!
    To help you consume your web services too.!

    View Slide

  25. !
    !
    ♫ If more PHP poetry you think you can swallow,!
    ♫ On Twitter and Tumblr, @phpbard you should follow.!

    View Slide

  26. 4!
    Guzzle!
    What's New?!

    View Slide

  27. What's New in Guzzle 4?!
    •  Improved performance!
    •  Simpler interfaces!
    •  Smaller core library!
    •  Easier event system!
    •  Requires PHP 5.4!
    •  No longer requires cURL!
    •  Custom service descriptions!

    View Slide

  28. Parallel Requests are Better!
     
    $client-­‐>sendAll($requests,  [  
           'complete'  =>  function(CompleteEvent  $e)  {  
                   //  Do  something  
           },  
           'error'  =>  function(ErrorEvent  $e)  {  
                   //  Do  something  
           }  
    ]);  

    View Slide

  29. Parallel Requests are Better!
    V3!
    V4!

    View Slide

  30. Parallel Requests are Better!
    Rolling Queues  
    V3!
    V4!

    View Slide

  31. Guzzle Resources – V3 vs. V4!
    V3   V4  
    Namespace   Guzzle! GuzzleHttp!
    GitHub   guzzle/guzzle3! guzzle/guzzle!
    Package   guzzle/guzzle! guzzlehttp/guzzle!
    Docs   guzzle3.readthedocs.org! guzzlephp.org!
    This  allows  you  to  use  Guzzle  3  and  4  in  the  same  project.  
    (And  this  has  actually  happened,  too)  

    View Slide

  32. Guzzle 4 Packages!
    •  guzzlehttp/guzzle!
    •  guzzlehttp/streams!
    •  guzzlehttp/command!
    •  guzzlehttp/guzzle-services!
    •  guzzlehttp/retry-subscriber!
    •  guzzlehttp/log-subscriber!
    •  guzzlehttp/message-integrity-subscriber!
    •  guzzlehttp/oauth-subscriber!
    •  guzzlehttp/progress-subscriber!

    View Slide

  33. Guzzle 4 Packages!
    •  guzzlehttp/guzzle!
    •  guzzlehttp/streams!
    •  guzzlehttp/command!
    •  guzzlehttp/guzzle-services!
    •  guzzlehttp/retry-subscriber!
    •  guzzlehttp/log-subscriber!
    •  guzzlehttp/message-integrity-subscriber!
    •  guzzlehttp/oauth-subscriber!
    •  guzzlehttp/progress-subscriber!
    Guzzle HTTP Layer!

    View Slide

  34. Guzzle 4 Packages!
    •  guzzlehttp/guzzle!
    •  guzzlehttp/streams!
    •  guzzlehttp/command!
    •  guzzlehttp/guzzle-services!
    •  guzzlehttp/retry-subscriber!
    •  guzzlehttp/log-subscriber!
    •  guzzlehttp/message-integrity-subscriber!
    •  guzzlehttp/oauth-subscriber!
    •  guzzlehttp/progress-subscriber!
    Guzzle Service Layer!

    View Slide

  35. Guzzle 4 Packages!
    •  guzzlehttp/guzzle!
    •  guzzlehttp/streams!
    •  guzzlehttp/command!
    •  guzzlehttp/guzzle-services!
    •  guzzlehttp/retry-subscriber!
    •  guzzlehttp/log-subscriber!
    •  guzzlehttp/message-integrity-subscriber!
    •  guzzlehttp/oauth-subscriber!
    •  guzzlehttp/progress-subscriber!
    Guzzle Plugins!

    View Slide

  36. 5!
    Guzzle!
    What's New?!

    View Slide

  37. What's New in Guzzle 5?!
    •  Non-blocking "Future" responses!
    – Makes true async requests possible!
    – Compatible with things like ReactPHP!
    •  New guts based on guzzle/guzzle-ring!
    •  Retry subscriber no longer blocking!
    •  New "progress" event for tracking!
    upload/download progress.!

    View Slide

  38. Looking for a Guzzle Example?!
    Basic Twilio client implementation!
    http://ow.ly/x7ugf!

    View Slide

  39. Guzzle PHP!
    Makes HTTP Easy!
    by @jeremeamia • for @NomadPHP!

    View Slide