Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

Hi, I’m Jeremy.! @jeremeamia!

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

Hi, I’m Jeremy.! @jeremeamia! Seattle PHP User Group! @seaphp! AWS SDK for PHP! @awsforphp! @phpbard! phpbard.tumblr.com! pnwphp.com! (Sep 11th-12th)!

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

Guzzle! ! Thanks to! @mtdowling! for Guzzle!!

Slide 10

Slide 10 text

AWS SDK for PHP ! ! Built on Guzzle!!

Slide 11

Slide 11 text

HTTP!

Slide 12

Slide 12 text

Hypertext! Transfer Protocol! HTTP!

Slide 13

Slide 13 text

HTTP!

Slide 14

Slide 14 text

HTTP!

Slide 15

Slide 15 text

HTTP! Web Services! APIs! Service Oriented Architecture!

Slide 16

Slide 16 text

HTTP! •  RFC 2616 (1999)!

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

URLs! •  RFC 3986 (2005)!

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

HTTP! Message! Request! Response!

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

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

Slide 41

Slide 41 text

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

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

No content

Slide 44

Slide 44 text

No content

Slide 45

Slide 45 text

No content

Slide 46

Slide 46 text

No content

Slide 47

Slide 47 text

No content

Slide 48

Slide 48 text

No content

Slide 49

Slide 49 text

No content

Slide 50

Slide 50 text

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

Slide 51

Slide 51 text

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

Slide 52

Slide 52 text

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!

Slide 53

Slide 53 text

Really, It's Easy!!

Slide 54

Slide 54 text

Really, It's Easy!! same as!

Slide 55

Slide 55 text

NO! curl_setopt

Slide 56

Slide 56 text

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

Slide 57

Slide 57 text

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

Slide 58

Slide 58 text

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

Slide 59

Slide 59 text

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

Slide 60

Slide 60 text

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

Slide 61

Slide 61 text

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

Slide 62

Slide 62 text

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!

Slide 63

Slide 63 text

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!

Slide 64

Slide 64 text

Let's Build a Service Client for…!

Slide 65

Slide 65 text

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

Slide 66

Slide 66 text

No content

Slide 67

Slide 67 text

No content

Slide 68

Slide 68 text

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

Slide 69

Slide 69 text

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

Slide 70

Slide 70 text

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

Slide 71

Slide 71 text

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

Slide 72

Slide 72 text

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

Slide 73

Slide 73 text

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

Slide 74

Slide 74 text

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

Slide 75

Slide 75 text

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