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.

Jeremy Lindblom

May 22, 2014
Tweet

More Decks by Jeremy Lindblom

Other Decks in Programming

Transcript

  1. …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());  
  2. …really easy! $url  =  'https://api.github.com/user';   $users  =  GuzzleHttp\get($url,  [

             'auth'  =>  ['username',  'password']   ])-­‐>json();  
  3. …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…!
  4. …a web service client framework! $aws  =  Aws\Common\Aws::factory();   $s3

     =  $aws-­‐>get('s3');   $result  =  $s3-­‐>listObjects([          'Bucket'  =>  $bucket   ]);   $names  =  $result-­‐>getPath(          'Contents/*/Key'   );  
  5. …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!
  6. …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!
  7. Send me your data! I'll stream it all! I'll send

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

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

    the spec to the letter! Web service client in PHP! ♫!
  10. 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!
  11. 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!
  12. Parallel Requests are Better! Asynchronous handling!   $client-­‐>sendAll($requests,  [  

           'complete'  =>  function(CompleteEvent  $e)  {                  //  Do  something          },          'error'  =>  function(ErrorEvent  $e)  {                  //  Do  something          }   ]);  
  13. 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.  
  14. 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!
  15. 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!
  16. 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!
  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! Guzzle Plugins!
  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! For our client!!
  19. Guzzle 4 Packages! {      "require":  {    

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

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

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

     Client.php      -­‐  twilio-­‐api.php   >  vendor/   -­‐  .gitignore   -­‐  composer.json   -­‐  composer.lock   Guzzle service! description for! Twilio API!
  23. 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!
  24. Twilio Service Description! <?php  return  [        

     'baseUrl'  =>  'https://api.twilio.com',          'apiVersion'  =>  '2010-­‐04-­‐01',          'operations'  =>  [...],          'models'  =>  [...],   ];  
  25. 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!