Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

About Me @jeremeamia

Slide 5

Slide 5 text

Async API Interaction with Guzzle ● Guzzle ● cURL ● HTTP ● PSR-7 ● Async ● Promises ● Coroutines

Slide 6

Slide 6 text

A HTTP Client library for PHP Guzzle

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

My favorite thing about Guzzle is..

Slide 9

Slide 9 text

No CURLOPTs

Slide 10

Slide 10 text

What is Guzzle? GitHub Repository Composer Package guzzle/guzzle guzzlehttp/guzzle guzzle/psr7 guzzlehttp/psr7 guzzle/promises guzzlehttp/promises v6 Docs: http://guzzlephp.org

Slide 11

Slide 11 text

HyperText Transfer Protocol (a.k.a. what makes the interwebz work) HTTP

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

HTTP Message Interfaces PSR-7

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

● 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

Slide 23

Slide 23 text

PSR-7

Slide 24

Slide 24 text

PSR-7

Slide 25

Slide 25 text

PSR-7

Slide 26

Slide 26 text

Now, let’s get back to... Guzzle PHP + HTTP + PSR-7 = Guzzle

Slide 27

Slide 27 text

Using Guzzle – Instantiate

Slide 28

Slide 28 text

Using Guzzle – Requests

Slide 29

Slide 29 text

Using Guzzle – Requests (alt.)

Slide 30

Slide 30 text

Using Guzzle – Responses

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

Guzzle is Designed for Async

Slide 33

Slide 33 text

Guzzle is Designed for Async Requests

Slide 34

Slide 34 text

Using Guzzle Asynchronously

Slide 35

Slide 35 text

Main Ingredient of Async Awesomesauce “It’s a real wicked pissah!” – ⭑⭑⭑⭑⭑, Boston Herald Promises

Slide 36

Slide 36 text

What is Promise? “A promise (is an object that) represents the eventual result of an asynchronous operation.” — Promises/A+ Specification

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

Then...

Slide 39

Slide 39 text

Then...

Slide 40

Slide 40 text

Then – For Guzzle Requests

Slide 41

Slide 41 text

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

Slide 42

Slide 42 text

Simple Example #1 – Manipulate value

Slide 43

Slide 43 text

Simple Example #2 – Chained Promises … …

Slide 44

Slide 44 text

Simple Example #2 – Chained Promises

Slide 45

Slide 45 text

Concurrent Async Requests

Slide 46

Slide 46 text

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

Slide 47

Slide 47 text

Async and Promises are Awesome!

Slide 48

Slide 48 text

WAIT!!!

Slide 49

Slide 49 text

Wait – Coming Back from Async Land

Slide 50

Slide 50 text

So... how does all this stuff work?

Slide 51

Slide 51 text

Guzzle Powered by magic and rainbows

Slide 52

Slide 52 text

No content

Slide 53

Slide 53 text

HTTP Handlers ● CurlHandler – via ● MultiCurlHandler – via ● StreamHandler – via ● ProxyHandler – all of the above; default ● MockHandler – for testing ● (insert your custom handler here)

Slide 54

Slide 54 text

Interface for Handlers (and Middleware)

Slide 55

Slide 55 text

ReactPHP Guzzle Handler …

Slide 56

Slide 56 text

Example #1 S3 Uploads in the AWS SDK for PHP

Slide 57

Slide 57 text

No content

Slide 58

Slide 58 text

No content

Slide 59

Slide 59 text

No content

Slide 60

Slide 60 text

No content

Slide 61

Slide 61 text

No content

Slide 62

Slide 62 text

No content

Slide 63

Slide 63 text

No content

Slide 64

Slide 64 text

No content

Slide 65

Slide 65 text

Coroutines -ing your way to a better world

Slide 66

Slide 66 text

Coroutines -ing your way to a better world This is probably where I’ll lose you.

Slide 67

Slide 67 text

What is a Coroutine?

Slide 68

Slide 68 text

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

Slide 69

Slide 69 text

What are Coroutines in Async? ● Coroutines yield s and have the results sent back in to resume execution ● Coroutines are also s.

Slide 70

Slide 70 text

Why? Enables you to write asynchronous code logic as if it is procedural.

Slide 71

Slide 71 text

Example #2 API authentication workflow

Slide 72

Slide 72 text

N Y N Y Y N

Slide 73

Slide 73 text

No content

Slide 74

Slide 74 text

No content

Slide 75

Slide 75 text

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