Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

Guzzle! ! Thanks to! @mtdowling! for Guzzle!!

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

SOA! Service! Oriented! Architecture!

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

SOA! is an extension of! Separation of Concerns!

Slide 8

Slide 8 text

SOA! is normal, modern! web development!

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

Guzzle! is…!

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

…a web service client framework! $aws  =  Aws\Common\Aws::factory();   $s3  =  $aws-­‐>get('s3');   $result  =  $s3-­‐>listObjects([          'Bucket'  =>  $bucket   ]);   $names  =  $result-­‐>getPath(          'Contents/*/Key'   );  

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

Nooooo!! ♫!

Slide 27

Slide 27 text

4! Guzzle! What's New?!

Slide 28

Slide 28 text

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!

Slide 29

Slide 29 text

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!

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

Parallel Requests are Better! V3! V4!

Slide 32

Slide 32 text

Parallel Requests are Better! Rolling Queues   V3! V4!

Slide 33

Slide 33 text

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.  

Slide 34

Slide 34 text

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!

Slide 35

Slide 35 text

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!

Slide 36

Slide 36 text

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!

Slide 37

Slide 37 text

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!

Slide 38

Slide 38 text

4! Guzzle! Let's Build!!

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

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

Slide 41

Slide 41 text

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

Slide 42

Slide 42 text

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

Slide 43

Slide 43 text

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

Slide 44

Slide 44 text

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

Slide 45

Slide 45 text

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!

Slide 46

Slide 46 text

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

Slide 47

Slide 47 text

Twilio Docs!

Slide 48

Slide 48 text

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

Slide 49

Slide 49 text

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!

Slide 50

Slide 50 text

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