Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Asynchronous API Interaction with Guzzle

Asynchronous API Interaction with Guzzle

Guzzle is an HTTP client library in PHP that puts all the power of cURL at your fingertips. Part of cURL’s power comes from its “Multi” interface, which allows you to make concurrent HTTP requests using non-blocking I/O. Guzzle's HTTP handler implementation abstracts the complexity of cURL Multi into a simpler experience using Promises and PSR-7. Learn how to use Guzzle to work with HTTP requests and web service APIs in an asynchronous way, potentially saving you time and increasing the throughput of your PHP applications and services. (First delivered at ZendCon 2015)

Ca57a7cfac69ba3abf517470f3770aae?s=128

Jeremy Lindblom

October 21, 2015
Tweet

More Decks by Jeremy Lindblom

Other Decks in Programming

Transcript

  1. Asynchronous API Interaction with Guzzle by Jeremy Lindblom (@jeremeamia)

  2. None
  3. None
  4. About Me @jeremeamia

  5. Async API Interaction with Guzzle • Guzzle • cURL •

    HTTP • PSR-7 • Async • Promises • Coroutines
  6. A HTTP Client library for PHP Guzzle

  7. What is Guzzle? • HTTP Client for PHP • Wrapper

    for cURL • PSR-7 compliant • Popular PHP Lib ◦ 5700+ stars ◦ 15,000,000+ installs HTTP Request HTTP Response PHP Web Application Web Service
  8. My favorite thing about Guzzle is..

  9. No CURLOPTs

  10. What is Guzzle? GitHub Repository Composer Package guzzle/guzzle guzzlehttp/guzzle guzzle/psr7

    guzzlehttp/psr7 guzzle/promises guzzlehttp/promises v6 Docs: http://guzzlephp.org
  11. HyperText Transfer Protocol (a.k.a. what makes the interwebz work) HTTP

  12. None
  13. None
  14. None
  15. None
  16. None
  17. None
  18. None
  19. None
  20. HTTP Message Interfaces PSR-7

  21. None
  22. • FIG = Framework Interoperability Group • PSR = PHP

    Standards Recommendation ◦ PSR-0, PSR-4 – Autoloading ◦ PSR-1, PSR-2 – Coding style ◦ PSR-3 – Logging interface ◦ PSR-7 – HTTP Message Interfaces PHP-FIG
  23. PSR-7

  24. PSR-7

  25. PSR-7

  26. Now, let’s get back to... Guzzle PHP + HTTP +

    PSR-7 = Guzzle
  27. Using Guzzle – Instantiate

  28. Using Guzzle – Requests

  29. Using Guzzle – Requests (alt.)

  30. Using Guzzle – Responses

  31. None
  32. Guzzle is Designed for Async

  33. Guzzle is Designed for Async Requests

  34. Using Guzzle Asynchronously

  35. Main Ingredient of Async Awesomesauce “It’s a real wicked pissah!”

    – ⭑⭑⭑⭑⭑, Boston Herald Promises
  36. What is Promise? “A promise (is an object that) represents

    the eventual result of an asynchronous operation.” — Promises/A+ Specification
  37. What is Promise? • A pending promise may be resolved

    by being... ◦ Fulfilled with a result ◦ Rejected with a reason • A resolved promise is immutable • A promise has a method
  38. Then...

  39. Then...

  40. Then – For Guzzle Requests

  41. Then – Return Values of Callbacks When the callback... returns

    a new promise that is... Returns a value Fulfilled with that value Returns a Pending until that is resolved Throws an exception Rejected with that thrown exception
  42. Simple Example #1 – Manipulate value

  43. Simple Example #2 – Chained Promises … …

  44. Simple Example #2 – Chained Promises

  45. Concurrent Async Requests

  46. Old Guzzle (concurrency w/ batching) New Guzzle (concurrency w/ async)

  47. Async and Promises are Awesome!

  48. WAIT!!!

  49. Wait – Coming Back from Async Land

  50. So... how does all this stuff work?

  51. Guzzle Powered by magic and rainbows

  52. None
  53. HTTP Handlers • CurlHandler – via • MultiCurlHandler – via

    • StreamHandler – via • ProxyHandler – all of the above; default • MockHandler – for testing • (insert your custom handler here)
  54. Interface for Handlers (and Middleware)

  55. ReactPHP Guzzle Handler …

  56. Example #1 S3 Uploads in the AWS SDK for PHP

  57. None
  58. None
  59. None
  60. None
  61. None
  62. None
  63. None
  64. None
  65. Coroutines -ing your way to a better world

  66. Coroutines -ing your way to a better world This is

    probably where I’ll lose you.
  67. What is a Coroutine?

  68. What is a Coroutine? • A function that can suspend

    & resume its execution • Cooperates with another function (hence the “co”) • Uses the syntax • It’s what happens when Generators go Super Saiyan
  69. What are Coroutines in Async? • Coroutines yield s and

    have the results sent back in to resume execution • Coroutines are also s.
  70. Why? Enables you to write asynchronous code logic as if

    it is procedural.
  71. Example #2 API authentication workflow

  72. N Y N Y Y N

  73. None
  74. None
  75. Asynchronous API Interaction with Guzzle by Jeremy Lindblom (@jeremeamia)