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 a HTTP client library in PHP that puts all the power of cURL at your fingertips, but with a simple and intuitive interface. Learn how to use Guzzle to work with HTTP and web service APIs, and see how Guzzle's simple HTTP abstractions, flexible event system, and asynchronous request capabilities make Guzzle an amazing open source project that should be a permanent part of your PHP tool belt.

Jeremy Lindblom

February 05, 2015
Tweet

More Decks by Jeremy Lindblom

Other Decks in Programming

Transcript

  1. TODAY! 1.  HTTP Overview! 2.  Using Guzzle to send HTTP

    requests! 3.  Building a Web Service! Client with Guzzle!
  2. Hi, I’m Jeremy.! @jeremeamia! Seattle PHP User Group! @seaphp! AWS

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

    SDK for PHP! @awsforphp! @phpbard! phpbard.tumblr.com! pnwphp.com! (Sep 11th-12th)!
  4. HTTP Responses! HTTP/1.1 200 OK! Content-Type: application/json! Content-Length: 45! !

    {'first':'Jeremy','id':293,'last':'Lindblom'}! Version!
  5. HTTP Responses! HTTP/1.1 200 OK! Content-Type: application/json! Content-Length: 45! !

    {'first':'Jeremy','id':293,'last':'Lindblom'}! Status Code!
  6. Statuses! •  1xx – Informational! •  2xx – Success !

    •  3xx – Redirection! •  4xx – Client Error! •  5xx – Server Error!
  7. HTTP Responses! HTTP/1.1 200 OK! Content-Type: application/json! Content-Length: 45! !

    {'first':'Jeremy','id':293,'last':'Lindblom'}! Reason Phrase!
  8. Statuses! •  200 OK! •  301 Moved Permanently! •  400

    Bad Request! •  403 Forbidden! •  404 Not Found! •  500 Internal Server Error!
  9. HTTP Responses! HTTP/1.1 200 OK! Content-Type: application/json! Content-Length: 45! !

    {'first':'Jeremy','id':293,'last':'Lindblom'}! Headers!
  10. HTTP Responses! HTTP/1.1 200 OK! Content-Type: application/json! Content-Length: 45! !

    {'first':'Jeremy','id':293,'last':'Lindblom'}! Empty Line!
  11. Guzzle Features! •  Makes HTTP easy! •  Uses cURL –OR–

    PHP stream handler! •  Persistent connections! •  Concurrent & asynchronous requests! •  Easy to extend! •  Body/stream abstraction! •  Tools for building web service clients!
  12. Guzzle Popularity! Used by:! •  Drupal 8! •  Laravel! • 

    AWS SDK for PHP! •  Goutte! •  Tumblr API Client! (As of 2015-03-12)!
  13. A History of Guzzle (Part 1)! 2011! APR! Guzzle! 1.0!

    Jeremy! finds! Guzzle! JAN! Guzzle! 2.0! 2012! MAR! Whoa! This lib is awesome!!
  14. A History of Guzzle (Part 2)! MAY! Michael! joins! AWS!

    NOV! Guzzle! 3.0! 2012! AWS! SDK 2.0! OCT!
  15. AWS! SDK 3.0! beta! A History of Guzzle (Part 4)!

    MAR! Guzzle! 4.0! 2014! OCT! Guzzle! 5.0!
  16. Guzzle ! •  Guzzle 4! – Swappable/custom HTTP adapters! – Improved concurrent

    requests! •  Guzzle 5! – Asynchronous requests! •  Guzzle 6 (coming soon)! – PSR7 compliant interfaces! – Improved asynchronous requests! – Replace events with middleware!
  17. MOAR CODEZ PLZ!!!! •  Examples: https://github.com/ jeremeamia/sunshinephp-guzzle- examples! •  Guzzle

    Docs: guzzlephp.org! •  Service Description Docs: http:// guzzle3.readthedocs.org/webservice- client/guzzle-service-descriptions.html! •  Pretend Service: httpbin.org!
  18. This is a story about an elePHPant named Guzzle.! She

    consumes lots of data. But how? It's no puzzle.!
  19. From client to server, and from server to client,! Her

    requests and responses are RFC compliant.!
  20. And beyond the basics of just cookie and header.! There's

    much more to Guzzle that just makes her better:!
  21. To show you all that Guzzle can do! To help

    you consume your web services too.!