Slide 1

Slide 1 text

aRESTful Development with the Wordpress API By Jeremy Lindblom (@jeremeamia)

Slide 2

Slide 2 text

@jeremeamia

Slide 3

Slide 3 text

Stuff I Work On ✘ API Designs ✘ API Clients ( e.g., AWS SDK for PHP ) ✘ API Reviews ✘ API Documentation ✘ PHP Libraries ( e.g., Guzzle ) ✘ Testing & Code Quality

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

“There's always money in the banana stand!”

Slide 6

Slide 6 text

http://v2.wp-api.org

Slide 7

Slide 7 text

UI API

Slide 8

Slide 8 text

API

Slide 9

Slide 9 text

API

Slide 10

Slide 10 text

“Headless” WordPress

Slide 11

Slide 11 text

“Come on!”

Slide 12

Slide 12 text

Getting Started Installing Support for the WP REST API

Slide 13

Slide 13 text

Step 1 Step 2

Slide 14

Slide 14 text

Step 3

Slide 15

Slide 15 text

Step 4 Code

Slide 16

Slide 16 text

Step 4 Code composer require guzzlehttp/guzzle

Slide 17

Slide 17 text

require 'vendor/autoload.php'; $client = new GuzzleHttp\Client([ 'base_uri' => $host . '/wp-json/wp/v2/', 'auth' => ['test', 'j6gk 1Jsh uokR y5vT'], ]); $response = $client->get('posts'); $result = json_decode($response->getBody()); Step 4

Slide 18

Slide 18 text

Array( [0] => Array( [id] => 6 [date] => 2016-05-11T04:12:29 [date_gmt] => 2016-05-11T04:12:29 [guid] => Array( [rendered] => http://example.com/?p=6 ) [modified] => 2016-05-11T04:12:29 [modified_gmt] => 2016-05-11T04:12:29 [slug] => great-post [type] => post [link] => http://example.com/2016/05/11/great-post/ [title] => Array( [rendered] => Great Post ) ... Step 5: Profit!

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

The Nitty-Gritties Details About How the API Works

Slide 21

Slide 21 text

API Docs

Slide 22

Slide 22 text

API Docs

Slide 23

Slide 23 text

Supported Resources ✘ Posts ✘ Post Revisions ✘ Pages ✘ Media ✘ Post Types ✘ Post Statuses ✘ Comments ✘ Taxonomies ✘ Categories ✘ Tags ✘ Users

Slide 24

Slide 24 text

Supported Operations ✘ Create ⇒ POST ✘ Read ⇒ GET ✘ Update ⇒ POST ✘ Delete ⇒ DELETE ✘ List ⇒ GET

Slide 25

Slide 25 text

General Parameters ✘ _jsonp ✘ _method ✘ _envelope ✘ _embed

Slide 26

Slide 26 text

Have any of you heard of the “Hypertext Application Language”?

Slide 27

Slide 27 text

Have any of you heard of the “Hypertext Application Language”? “I don’t understand the question and I won’t respond to it.”

Slide 28

Slide 28 text

Hypermedia Linking Resources With the HAL Spec

Slide 29

Slide 29 text

HAL = Hypertext Application Language Spec for hyperlinking resources Makes your API “explorable” “Simple” format to consume Uses _links & _embedded

Slide 30

Slide 30 text

require 'vendor/autoload.php'; $client = new GuzzleHttp\Client([...]); $response = $client->get('posts/6'); $result = json_decode($response->getBody());

Slide 31

Slide 31 text

[id] => 6 [slug] => great-post ... [_links] => Array( [collection] => Array( [0] => Array( [href] => http://demo.wp-api.org/wp-json/wp/v2/posts ) ) [author] => Array( [0] => Array( [embeddable] => 1 [href] => http://demo.wp-api.org/wp-json/wp/v2/users/1 ) ) ... )

Slide 32

Slide 32 text

“I’m here to see the magic”

Slide 33

Slide 33 text

$client->get('posts/6?_embed=1');

Slide 34

Slide 34 text

[id] => 6 [slug] => great-post ... [_links] => Array(...) [_embedded] => Array( [author] => Array( [0] => Array( [id] => 1 [name] => Jeremy Lindblom [slug] => jeremeamia ... [_links] => Array(...) ) ) )

Slide 35

Slide 35 text

[id] => 6 [slug] => great-post ... [_links] => Array(...) [_embedded] => Array( [author] => Array( [0] => Array( [id] => 1 [name] => Jeremy Lindblom [slug] => jeremeamia ... [_links] => Array(...) ) ) ) Yay! Recursion!

Slide 36

Slide 36 text

[id] => 6 [slug] => great-post ... [_links] => Array(...) [_embedded] => Array( [author] => Array( [0] => Array( [id] => 1 [name] => Jeremy Lindblom [slug] => jeremeamia ... [_links] => Array(...) ) ) ) “Simple” “Explorable”

Slide 37

Slide 37 text

“I’m a monster!”

Slide 38

Slide 38 text

HAL Data Model

Slide 39

Slide 39 text

Authentication Giving Your Consumers Access

Slide 40

Slide 40 text

Types of Authentication ✘ Cookie Authentication ✘ OAuth Authentication ✘ Application Passwords ✘ Basic Auth

Slide 41

Slide 41 text

“ I was never really clear on that.”

Slide 42

Slide 42 text

Types of Authentication ✘ Cookie Authentication ✘ OAuth Authentication ✘ Application Passwords ✘ Basic Auth

Slide 43

Slide 43 text

require 'vendor/autoload.php'; $client = new GuzzleHttp\Client([ 'base_uri' => $host . '/wp-json/wp/v2/', 'auth' => ['test', 'j6gk 1Jsh uokR y5vT'], ]); $response = $client->get('posts'); $result = json_decode($response->getBody());

Slide 44

Slide 44 text

GET /wp-json/wp/v2/posts HTTP/1.1 Host: http://demo.wp-api.org User-Agent: GuzzleHttp/6.2.0 curl/7.43.0 PHP/5.6.12 Authorization: Basic dGVzdC1jbGllbnQ6ZlpuBrYyIHNtQjkgWmtqVw== HTTP Basic Authentication

Slide 45

Slide 45 text

GET /wp-json/wp/v2/posts HTTP/1.1 Host: http://demo.wp-api.org User-Agent: GuzzleHttp/6.2.0 curl/7.43.0 PHP/5.6.12 Authorization: Basic dGVzdC1jbGllbnQ6ZlpuBrYyIHNtQjkgWmtqVw== base64_encode(“{$username}:{$password}”) e.g., base64_encode(‘test:j6gk 1Jsh uokR y5vT’)

Slide 46

Slide 46 text

“hot ham water”

Slide 47

Slide 47 text

Types of Authentication ✘ Cookie Authentication ✘ OAuth Authentication ✘ Application Passwords ✘ Basic Auth

Slide 48

Slide 48 text

Types of Authentication ➔ Cookies + NONCE ➔ Designed for JavaScript ➔ Use within themes/plugins ➔ Session-based ➔ Safe for HTTP ✘ Cookie Authentication ✘ OAuth Authentication ✘ Application Passwords ✘ Basic Auth

Slide 49

Slide 49 text

Types of Authentication ➔ 3-legged OAuth 1.0a ➔ Requires Plugin ➔ Session-based ➔ Safe for HTTP ➔ Most recommended ✘ Cookie Authentication ✘ OAuth Authentication ✘ Application Passwords ✘ Basic Auth

Slide 50

Slide 50 text

Types of Authentication ➔ HTTP Basic Auth ➔ Requires Plugin ➔ Application-based ➔ USE HTTPS! ➔ Easily Revokable ✘ Cookie Authentication ✘ OAuth Authentication ✘ Application Passwords ✘ Basic Auth

Slide 51

Slide 51 text

Types of Authentication ➔ HTTP Basic Auth ➔ Requires Plugin ➔ Application-based ➔ USE HTTPS! ➔ Uses Admin’s credentials! ✘ Cookie Authentication ✘ OAuth Authentication ✘ Application Passwords ✘ Basic Auth

Slide 52

Slide 52 text

GET /wp-json/wp/v2/posts HTTP/1.1 Host: http://demo.wp-api.org User-Agent: GuzzleHttp/6.2.0 curl/7.43.0 PHP/5.6.12 Authorization: Basic aGVz47gjbGllbcf9ZlpuBrYIHNtQjkgWmtqVw8= base64_encode(admin:p@$$w0rd12345’)

Slide 53

Slide 53 text

“I’ve made a huge mistake”

Slide 54

Slide 54 text

Types of Authentication ➔ Easy setup ➔ Requires Plugin ➔ HTTP Basic Auth ➔ Application-based ➔ USE HTTPS! ➔ DEVELOPMENT ONLY! DO NOT DISTRIBUTE YOUR CREDENTIALS! ✘ Cookie Authentication ✘ OAuth Authentication ✘ Application Passwords ✘ Basic Auth

Slide 55

Slide 55 text

Tools & Resources Working Effectively with the Wordpress API

Slide 56

Slide 56 text

Tools ✘ Wordpress itself

Slide 57

Slide 57 text

$auth = base64_encode("{$user}:{$pass}"); $response = wp_remote_post( rest_url('wp/v2/posts/1'), [ 'method' => 'POST', 'headers' => [ 'Authorization' => "Basic {$auth}", ], 'body' => $data ] );

Slide 58

Slide 58 text

wp.api.loadPromise.done(function() { var post = new wp.api.models.Post({ title: 'This is a test post' }); post.save(); });

Slide 59

Slide 59 text

Tools ✘ Wordpress itself ✘ Recommended in Docs: ✗ https://github.com/WP-API/example-client ✗ https://github.com/WP-API/client-cli ✗ https://github.com/WP-API/api-console ✘ HTTP Clients (Guzzle, cURL, etc.)

Slide 60

Slide 60 text

No content

Slide 61

Slide 61 text

THANKS! Any questions? You can find me at... ✘ @jeremeamia ✘ [email protected] ✘ the @azPHP Meetups