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 full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  5. SOA!
    Service!
    Oriented!
    Architecture!

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  10. Guzzle!
    is…!

    View full-size slide

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

    View full-size 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 full-size slide

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

    View full-size 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 full-size 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 full-size 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 full-size 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 full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  26. Nooooo!!
    ♫!

    View full-size slide

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

    View full-size 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 full-size 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 full-size slide

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

    View full-size slide

  31. Parallel Requests are Better!
    V3!
    V4!

    View full-size slide

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

    View full-size 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 full-size 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 full-size 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 full-size 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 full-size 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 full-size slide

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

    View full-size slide

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

    View full-size 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 full-size slide

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

    View full-size slide

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

    View full-size slide

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

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

    View full-size 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 full-size 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 full-size slide

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

    View full-size slide

  47. Twilio Docs!

    View full-size slide

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

    View full-size 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 full-size slide

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

    View full-size slide