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. Speak HTTP and Consume APIs!
    Guzzle!
    by @jeremeamia • #mwphp15!
    with!

    View Slide

  2. TODAY!
    1.  HTTP Overview!
    2.  Using Guzzle to send
    HTTP requests!
    3.  Building a Web Service!
    Client with Guzzle!

    View Slide

  3. Hi, I’m Jeremy.!
    @jeremeamia!

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  8. Guzzle!
    !
    HTTP Client!
    - & -!
    Web Service!
    Client!
    Framework!

    View Slide

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

    View Slide

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

    View Slide

  11. HTTP!

    View Slide

  12. Hypertext!
    Transfer Protocol!
    HTTP!

    View Slide

  13. HTTP!

    View Slide

  14. HTTP!

    View Slide

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

    View Slide

  16. HTTP!
    •  RFC 2616 (1999)!

    View Slide

  17. View Slide

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

    View Slide

  19. HTTP!
    •  RFC 2616 (1999)!
    •  RFC 723* (2014)!
    •  HTTP/2 (2015)!
    ( http2.github.io )!

    View Slide

  20. URLs!
    •  RFC 3986 (2005)!

    View Slide

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

    View Slide

  22. HTTP!
    Message!
    Request! Response!

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  26. HTTP Methods!
    GET!
    POST!
    PUT!
    DELETE!
    !
    HEAD!
    OPTIONS!
    PATCH!
    etc.!

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  35. Statuses!
    •  1xx – Informational!
    •  2xx – Success !
    •  3xx – Redirection!
    •  4xx – Client Error!
    •  5xx – Server Error!

    View Slide

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

    View Slide

  37. Statuses!
    •  200 OK!
    •  301 Moved Permanently!
    •  400 Bad Request!
    •  403 Forbidden!
    •  404 Not Found!
    •  500 Internal Server Error!

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  41. Header Effects!
    Request:!
    Accept: application/json!
    !
    !
    ê!
    !
    Response:!
    Content-Type: application/json!

    View Slide

  42. View Slide

  43. View Slide

  44. View Slide

  45. View Slide

  46. View Slide

  47. View Slide

  48. View Slide

  49. View Slide

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

    View Slide

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

    View Slide

  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!

    View Slide

  53. Really, It's Easy!!

    View Slide

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

    View Slide

  55. NO!
    curl_setopt

    View Slide

  56. Guzzle Popularity!
    Used by:!
    •  Drupal 8!
    •  Laravel!
    •  AWS SDK for PHP!
    •  Goutte!
    •  Tumblr API Client!
    (As of 2015-03-12)!

    View Slide

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

    View Slide

  58. A History of Guzzle (Part 2)!
    MAY!
    Michael!
    joins!
    AWS!
    NOV!
    Guzzle!
    3.0!
    2012!
    AWS!
    SDK 2.0!
    OCT!

    View Slide

  59. A History of Guzzle (Part 3)!
    JAN! DEC!
    2013!
    Regular Updates!
    |!
    )!
    )!

    View Slide

  60. AWS!
    SDK 3.0!
    beta!
    A History of Guzzle (Part 4)!
    MAR!
    Guzzle!
    4.0!
    2014!
    OCT!
    Guzzle!
    5.0!

    View Slide

  61. Coming Soon!
    APR/MAY?!
    2015!
    AWS!
    SDK 3.0!
    stable!
    Guzzle!
    6.0!

    View Slide

  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!

    View Slide

  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!

    View Slide

  64. Let's Build a Service Client for…!

    View Slide

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

    View Slide

  66. View Slide

  67. View Slide

  68. And now!
    a story about
    Guzzle by!
    @phpbard!

    View Slide

  69. This is a story about an elePHPant named Guzzle.!
    She consumes lots of data. But how? It's no puzzle.!

    View Slide

  70. Armed with HTTP, she knows how to transport!
    Text and files of any size and sort.!

    View Slide

  71. From client to server, and from server to client,!
    Her requests and responses are RFC compliant.!

    View Slide

  72. And beyond the basics of just cookie and header.!
    There's much more to Guzzle that just makes her better:!

    View Slide

  73. From concurrent requests, from events and plugins,!
    To web service descriptions. And this just begins…!

    View Slide

  74. To show you all that Guzzle can do!
    To help you consume your web services too.!

    View Slide

  75. Speak HTTP and Consume APIs!
    Guzzle!
    by @jeremeamia • #ssp15!
    with!

    View Slide