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.

Ca57a7cfac69ba3abf517470f3770aae?s=128

Jeremy Lindblom

February 05, 2015
Tweet

Transcript

  1. Speak HTTP and Consume APIs! Guzzle! by @jeremeamia • #mwphp15!

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

    requests! 3.  Building a Web Service! Client with Guzzle!
  3. Hi, I’m Jeremy.! @jeremeamia!

  4. Hi, I’m Jeremy.! @jeremeamia! Seattle PHP User Group! @seaphp!

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

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

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

    SDK for PHP! @awsforphp! @phpbard! phpbard.tumblr.com! pnwphp.com! (Sep 11th-12th)!
  8. Guzzle! ! HTTP Client! - & -! Web Service! Client!

    Framework!
  9. Guzzle! ! Thanks to! @mtdowling! for Guzzle!!

  10. AWS SDK for PHP ! ! Built on Guzzle!!

  11. HTTP!

  12. Hypertext! Transfer Protocol! HTTP!

  13. HTTP!

  14. HTTP!

  15. HTTP! Web Services! APIs! Service Oriented Architecture!

  16. HTTP! •  RFC 2616 (1999)!

  17. None
  18. HTTP! •  RFC 2616 (1999)! •  RFC 723* (2014)!

  19. HTTP! •  RFC 2616 (1999)! •  RFC 723* (2014)! • 

    HTTP/2 (2015)! ( http2.github.io )!
  20. URLs! •  RFC 3986 (2005)!

  21. URLs! http://example.com:9000/a/b/c?z=10#foo! ! •  scheme! •  authority = userinfo @

    host : port! •  path! •  query! •  fragment!
  22. HTTP! Message! Request! Response!

  23. HTTP Messages! START_LINE CRLF! (MESSAGE_HEADERS CRLF)*! CRLF! [MESSAGE_BODY]!

  24. HTTP Requests! POST /people HTTP/1.1! Host: example.com! Accept: application/json! Content-Length:

    26! ! first=Jeremy&last=Lindblom!
  25. HTTP Requests! POST /people HTTP/1.1! Host: example.com! Accept: application/json! Content-Length:

    26! ! first=Jeremy&last=Lindblom! ! Method!
  26. HTTP Methods! GET! POST! PUT! DELETE! ! HEAD! OPTIONS! PATCH!

    etc.!
  27. HTTP Requests! POST /people HTTP/1.1! Host: example.com! Accept: application/json! Content-Length:

    26! ! first=Jeremy&last=Lindblom! ! Path!
  28. HTTP Requests! POST /people HTTP/1.1! Host: example.com! Accept: application/json! Content-Length:

    26! ! first=Jeremy&last=Lindblom! ! Version!
  29. HTTP Requests! POST /people HTTP/1.1! Host: example.com! Accept: application/json! Content-Length:

    26! ! first=Jeremy&last=Lindblom! ! Headers!
  30. HTTP Requests! POST /people HTTP/1.1! Host: example.com! Accept: application/json! Content-Length:

    26! ! first=Jeremy&last=Lindblom! ! Empty Line!
  31. HTTP Requests! POST /people HTTP/1.1! Host: example.com! Accept: application/json! Content-Length:

    26! ! first=Jeremy&last=Lindblom! ! Body!
  32. HTTP Responses! HTTP/1.1 200 OK! Content-Type: application/json! Content-Length: 45! !

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

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

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

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

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

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

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

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

    {'first':'Jeremy','id':293,'last':'Lindblom'}! Body!
  41. Header Effects! Request:! Accept: application/json! ! ! ê! ! Response:!

    Content-Type: application/json!
  42. None
  43. None
  44. None
  45. None
  46. None
  47. None
  48. None
  49. None
  50. PHP as the HTTP Client! • fsockopen()! • file_get_contents()! • cURL! • PECL_HTTP! • PHP

    libraries (e.g., Guzzle)!
  51. PHP as the HTTP Client! • fsockopen()! • file_get_contents()! • cURL! • PECL_HTTP! • PHP

    libraries (e.g., Guzzle)!
  52. 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!
  53. Really, It's Easy!!

  54. Really, It's Easy!! same as!

  55. NO! curl_setopt

  56. Guzzle Popularity! Used by:! •  Drupal 8! •  Laravel! • 

    AWS SDK for PHP! •  Goutte! •  Tumblr API Client! (As of 2015-03-12)!
  57. 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!!
  58. A History of Guzzle (Part 2)! MAY! Michael! joins! AWS!

    NOV! Guzzle! 3.0! 2012! AWS! SDK 2.0! OCT!
  59. A History of Guzzle (Part 3)! JAN! DEC! 2013! Regular

    Updates! |! )! )!
  60. AWS! SDK 3.0! beta! A History of Guzzle (Part 4)!

    MAR! Guzzle! 4.0! 2014! OCT! Guzzle! 5.0!
  61. Coming Soon! APR/MAY?! 2015! AWS! SDK 3.0! stable! Guzzle! 6.0!

  62. 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!
  63. 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!
  64. Let's Build a Service Client for…!

  65. Guzzle Packages! {! "require": {! "guzzlehttp/guzzle": "~5.0",! "guzzlehttp/guzzle-services": "~0.5.0",! "guzzlehttp/retry-subscriber":

    "~2.0",! },! …! }!
  66. None
  67. None
  68. And now! a story about Guzzle by! @phpbard!

  69. This is a story about an elePHPant named Guzzle.! She

    consumes lots of data. But how? It's no puzzle.!
  70. Armed with HTTP, she knows how to transport! Text and

    files of any size and sort.!
  71. From client to server, and from server to client,! Her

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

    much more to Guzzle that just makes her better:!
  73. From concurrent requests, from events and plugins,! To web service

    descriptions. And this just begins…!
  74. To show you all that Guzzle can do! To help

    you consume your web services too.!
  75. Speak HTTP and Consume APIs! Guzzle! by @jeremeamia • #ssp15!

    with!