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

Building Web Service Clients with Guzzle

Building Web Service Clients with Guzzle

Guzzle is an HTTP client and a framework for building web service clients. Guzzle puts all the power of cURL at your fingertips with a simple and intuitive interface. Learn how to use Guzzle to create a web service client, and see how features like persistent and parallel connections, service descriptions and commands, and the event-driven plugin system make Guzzle an amazing open source project that should be a permanent part of your PHP tool belt.

Ca57a7cfac69ba3abf517470f3770aae?s=128

Jeremy Lindblom

May 22, 2014
Tweet

Transcript

  1. Building Web Service Clients! Guzzle! by @jeremeamia • for php[tek]2014

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

    SDK for PHP! @awsforphp!
  3. Guzzle! ! Thanks to! @mtdowling! for Guzzle!!

  4. 4! Guzzle! Version 4 – Now Available!! ! Version 4

    – Now Available!! .!
  5. SOA! Service! Oriented! Architecture!

  6. SOA! Web Services! SaaS! PaaS! IaaS! Cloud! Scalability! APIs! Widgets!

    OAuth! Distributed! REST! Hypermedia!
  7. SOA! is an extension of! Separation of Concerns!

  8. SOA! is normal, modern! web development!

  9. SOA! •  Login with Google/Twitter/Facebook/etc.! •  AWS, Twilio, MailChimp, Dropbox,

    etc.! •  REST, APIs, Backbone/Angular/etc., Mobile!
  10. Guzzle! is…!

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

  12. …an HTTP client! $client  =  new  GuzzleHttp\Client([      

       'base_url'  =>  'https://api.github.com'   ]);   $response  =  $client-­‐>get('user',  [          'auth'  =>  ['username',  'password']   ]);   echo  $response-­‐>getStatusCode();   echo  $response-­‐>getBody();   print_r($response-­‐>json());  
  13. …really easy! $url  =  'https://api.github.com/user';   $users  =  GuzzleHttp\get($url,  [

             'auth'  =>  ['username',  'password']   ])-­‐>json();  
  14. …packed with features! •  The power of cURL w/ a

    simple interface! •  Can send requests serially or in parallel! •  Provides event hooks & plugins for mocks, cookies, caching, logging, OAuth, etc.! •  Keep-Alive & connection pooling! •  And more…!
  15. …a web service client framework! $aws  =  Aws\Common\Aws::factory();   $s3

     =  $aws-­‐>get('s3');   $result  =  $s3-­‐>listObjects([          'Bucket'  =>  $bucket   ]);   $names  =  $result-­‐>getPath(          'Contents/*/Key'   );  
  16. …popular! •  Nearly 3400+ stars on GitHub! •  Used in

    many other projects including! – Drupal 8! – Goutte! – AWS SDK for PHP! – Laravel Mail Component! – Tumblr! – Many more J!
  17. …a well-crafted PHP library! •  Follow good OOP patterns and

    practices! •  Follows PSR-1, 2, 3, & 4! •  Uses Git, Composer, Travis CI, PHPUnit! •  High test coverage! •  Extensible via DI and the event system!
  18. "Web Service Client in PHP"! To the tune of Journey's!

    "Separate Ways"! ♫!
  19. Here we stand! Nodes apart! Code factored in two! ♫!

  20. APIs! Using Guzzle! Talkin' to you! ♫!

  21. Send me your data! I'll stream it all! I'll send

    you my parameters! With your protocol! Answer my call! ♫!
  22. Guzzle sends my request! To use cURL, Guzzle's the best!

    HTTP and REST! Web service client in PHP! ♫!
  23. If you're sending a header! Guzzle does it better! Follows

    the spec to the letter! Web service client in PHP! ♫!
  24. We still love you cURL! We still need you cURL!

    ♫!
  25. But when cURL tries to hurt you! Guzzle won't desert

    you! ♫!
  26. Nooooo!! ♫!

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

  28. What's New in Guzzle 4?! •  Improved performance! •  Simpler

    interfaces! •  Smaller core library! •  Event system is improved! •  Requires PHP 5.4! •  Custom service descriptions! •  Actually follows SemVer now!
  29. Swappable HTTP Adapters! •  Curl\MultiAdapter   cURL is enabled! • 

    Curl\CurlAdapter   PHP 5.5+ required (cURL "easy " handles)! •  StreamingAdapter   no cURL, allow_url_fopen is enabled! •  StreamingProxyAdapter   cURL & allow_url_fopen are enabled!
  30. Parallel Requests are Better! Asynchronous handling!   $client-­‐>sendAll($requests,  [  

           'complete'  =>  function(CompleteEvent  $e)  {                  //  Do  something          },          'error'  =>  function(ErrorEvent  $e)  {                  //  Do  something          }   ]);  
  31. Parallel Requests are Better! V3! V4!

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

  33. 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.  
  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!
  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 HTTP Layer!
  36. 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!
  37. 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!
  38. 4! Guzzle! Let's Build!!

  39. What are we building?! A web service client for…! !

  40. 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! For our client!!
  41. Guzzle 4 Packages! {      "require":  {    

         "guzzlehttp/guzzle-­‐services":  "~0.2.0",          "guzzlehttp/retry-­‐subscriber":  "~0.1.0"      },      "autoload":{"psr-­‐4":{"Twilio\\":"src/"}}   }  
  42. Project Structure! (after composer install)! >  src/      -­‐

     Client.php      -­‐  twilio-­‐api.php   >  vendor/   -­‐  .gitignore   -­‐  composer.json   -­‐  composer.lock  
  43. Project Structure! (after composer install)! >  src/      -­‐

     Client.php      -­‐  twilio-­‐api.php   >  vendor/   -­‐  .gitignore   -­‐  composer.json -­‐  composer.lock   Twilio Client! (extends Guzzle)!
  44. Project Structure! (after composer install)! >  src/      -­‐

     Client.php      -­‐  twilio-­‐api.php   >  vendor/   -­‐  .gitignore   -­‐  composer.json   -­‐  composer.lock   Guzzle service! description for! Twilio API!
  45. Guzzle Service Descriptions! •  DSL for describing web service operations!

    •  Defines where parameters go! (e.g., uri, query string, post field, json body)! •  Defines how response data is represented! •  Similar to Swagger! •  Guzzle 4's service descriptions are still beta!
  46. Twilio Service Description! <?php  return  [        

     'baseUrl'  =>  'https://api.twilio.com',          'apiVersion'  =>  '2010-­‐04-­‐01',          'operations'  =>  [...],          'models'  =>  [...],   ];  
  47. Twilio Docs!

  48. Twilio Client Implementation! Let's take a look at! the source

    code. J! http://ow.ly/x7ugf! !
  49. Summary! Guzzle is an awesome HTTP client! — & —!

    A web service client framework! ! Check out version 4 of Guzzle! ! Writing a web service client can be easy!
  50. Building Web Service Clients! Guzzle! By: @jeremeamia! Feedback: https://joind.in/10638! Guzzle:

    http://guzzlephp.org! Questions?! with!