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. 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!
  2. …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!
  3. …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());  
  4. …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]);    
  5. …an HTTP client! $r  =  $client-­‐>METHOD('PATH',  [      

       'headers'  =>  […],          'body'        =>  […],          'query'      =>  […],          'cookies'  =>  […],          'timeout'  =>  […],          'proxy'      =>  […],      //  …   ]);    
  6. …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!
  7. …a web service client framework! $s3  =  Aws\S3\S3Client::factory();   $result

     =  $s3-­‐>listObjects([          'Bucket'  =>  $bucket   ]);   foreach  ($result['Contents']  as  $obj)  {          echo  $obj['Key'];   );  
  8. …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!
  9. This is a story about an elePHPant named Guzzle.! She

    consumes lots of data. But how? It's no puzzle.!
  10. From client to server, and from server to client,! Her

    requests and responses are RFC compliant.!
  11. And beyond the basics of just cookie and header.! There's

    much more to Guzzle that just makes her better:!
  12. To show you all that Guzzle can do! To help

    you consume your web services too.!
  13. ! ! ♫ If more PHP poetry you think you

    can swallow,! ♫ On Twitter and Tumblr, @phpbard you should follow.!
  14. 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!
  15. Parallel Requests are Better!   $client-­‐>sendAll($requests,  [      

       'complete'  =>  function(CompleteEvent  $e)  {                  //  Do  something          },          'error'  =>  function(ErrorEvent  $e)  {                  //  Do  something          }   ]);  
  16. 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)  
  17. 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!
  18. 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!
  19. 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!
  20. 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!
  21. 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.!