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)

Jeremy Lindblom

October 21, 2015
Tweet

More Decks by Jeremy Lindblom

Other Decks in Programming

Transcript

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

    HTTP • PSR-7 • Async • Promises • Coroutines
  2. 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
  3. What is Guzzle? GitHub Repository Composer Package guzzle/guzzle guzzlehttp/guzzle guzzle/psr7

    guzzlehttp/psr7 guzzle/promises guzzlehttp/promises v6 Docs: http://guzzlephp.org
  4. • 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
  5. What is Promise? “A promise (is an object that) represents

    the eventual result of an asynchronous operation.” — Promises/A+ Specification
  6. 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
  7. 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
  8. HTTP Handlers • CurlHandler – via • MultiCurlHandler – via

    • StreamHandler – via • ProxyHandler – all of the above; default • MockHandler – for testing • (insert your custom handler here)
  9. Coroutines -ing your way to a better world This is

    probably where I’ll lose you.
  10. 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
  11. What are Coroutines in Async? • Coroutines yield s and

    have the results sent back in to resume execution • Coroutines are also s.