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.

Ca57a7cfac69ba3abf517470f3770aae?s=128

Jeremy Lindblom

September 18, 2014
Tweet

Transcript

  1. 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!
  2. 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!
  3. 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());  
  4. 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]);    
  5. 13.

    …an HTTP client! $r  =  $client-­‐>METHOD('PATH',  [      

       'headers'  =>  […],          'body'        =>  […],          'query'      =>  […],          'cookies'  =>  […],          'timeout'  =>  […],          'proxy'      =>  […],      //  …   ]);    
  6. 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!
  7. 16.

    …a web service client framework! $s3  =  Aws\S3\S3Client::factory();   $result

     =  $s3-­‐>listObjects([          'Bucket'  =>  $bucket   ]);   foreach  ($result['Contents']  as  $obj)  {          echo  $obj['Key'];   );  
  8. 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!
  9. 19.

    This is a story about an elePHPant named Guzzle.! She

    consumes lots of data. But how? It's no puzzle.!
  10. 21.

    From client to server, and from server to client,! Her

    requests and responses are RFC compliant.!
  11. 22.

    And beyond the basics of just cookie and header.! There's

    much more to Guzzle that just makes her better:!
  12. 23.
  13. 24.

    To show you all that Guzzle can do! To help

    you consume your web services too.!
  14. 25.

    ! ! ♫ If more PHP poetry you think you

    can swallow,! ♫ On Twitter and Tumblr, @phpbard you should follow.!
  15. 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!
  16. 28.

    Parallel Requests are Better!   $client-­‐>sendAll($requests,  [      

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