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. Building Web Service Clients!
    Guzzle!
    by @jeremeamia • for php[tek]2014 !
    with!

    View Slide

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

    View Slide

  3. Guzzle!
    !
    Thanks to!
    @mtdowling!
    for Guzzle!!

    View Slide

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

    View Slide

  5. SOA!
    Service!
    Oriented!
    Architecture!

    View Slide

  6. SOA!
    Web Services!
    SaaS!
    PaaS!
    IaaS!
    Cloud!
    Scalability!
    APIs!
    Widgets!
    OAuth!
    Distributed!
    REST!
    Hypermedia!

    View Slide

  7. SOA!
    is an extension of!
    Separation of Concerns!

    View Slide

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

    View Slide

  9. SOA!
    •  Login with Google/Twitter/Facebook/etc.!
    •  AWS, Twilio, MailChimp, Dropbox, etc.!
    •  REST, APIs, Backbone/Angular/etc., Mobile!

    View Slide

  10. Guzzle!
    is…!

    View Slide

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

    View Slide

  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());  

    View Slide

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

    View Slide

  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…!

    View Slide

  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'  
    );  

    View Slide

  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!

    View Slide

  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!

    View Slide

  18. "Web Service Client in PHP"!
    To the tune of Journey's!
    "Separate Ways"!
    ♫!

    View Slide

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

    View Slide

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

    View Slide

  21. Send me your data!
    I'll stream it all!
    I'll send you my parameters!
    With your protocol!
    Answer my call!
    ♫!

    View Slide

  22. Guzzle sends my request!
    To use cURL, Guzzle's the best!
    HTTP and REST!
    Web service client in PHP!
    ♫!

    View Slide

  23. If you're sending a header!
    Guzzle does it better!
    Follows the spec to the letter!
    Web service client in PHP!
    ♫!

    View Slide

  24. We still love you cURL!
    We still need you cURL!
    ♫!

    View Slide

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

    View Slide

  26. Nooooo!!
    ♫!

    View Slide

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

    View Slide

  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!

    View Slide

  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!

    View Slide

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

    View Slide

  31. Parallel Requests are Better!
    V3!
    V4!

    View Slide

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

    View Slide

  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.  

    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!

    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 HTTP Layer!

    View Slide

  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!

    View Slide

  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!

    View Slide

  38. 4!
    Guzzle!
    Let's Build!!

    View Slide

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

    View Slide

  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!!

    View Slide

  41. Guzzle 4 Packages!
    {  
       "require":  {  
           "guzzlehttp/guzzle-­‐services":  "~0.2.0",  
           "guzzlehttp/retry-­‐subscriber":  "~0.1.0"  
       },  
       "autoload":{"psr-­‐4":{"Twilio\\":"src/"}}  
    }  

    View Slide

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

    View Slide

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

    -­‐  composer.lock  
    Twilio Client!
    (extends Guzzle)!

    View Slide

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

    View Slide

  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!

    View Slide

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

    View Slide

  47. Twilio Docs!

    View Slide

  48. Twilio Client Implementation!
    Let's take a look at!
    the source code. J!
    http://ow.ly/x7ugf!
    !

    View Slide

  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!

    View Slide

  50. Building Web Service Clients!
    Guzzle!
    By: @jeremeamia!
    Feedback: https://joind.in/10638!
    Guzzle: http://guzzlephp.org!
    Questions?!
    with!

    View Slide