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

aRESTful Development with the Wordpress API

aRESTful Development with the Wordpress API

Don't make "a huge mistake"! You need to learn about the WordPress REST API—yet another way we can bend WordPress to our will and solve problems for our customers in new ways. Jeremy Lindblom (@jeremeamia) will show you how to get started with the WordPress API, and teach you a little about HTTP and APIs, in general, along the way.

You should walk away with "Steve Holt"-like confidence in knowing how to setup the API for a WordPress site and consume the API from other projects using existing tools.

Jeremy Lindblom

May 26, 2016
Tweet

More Decks by Jeremy Lindblom

Other Decks in Programming

Transcript

  1. 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
  2. API

  3. API

  4. 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
  5. 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!
  6. Supported Resources ✘ Posts ✘ Post Revisions ✘ Pages ✘

    Media ✘ Post Types ✘ Post Statuses ✘ Comments ✘ Taxonomies ✘ Categories ✘ Tags ✘ Users
  7. Supported Operations ✘ Create ⇒ POST ✘ Read ⇒ GET

    ✘ Update ⇒ POST ✘ Delete ⇒ DELETE ✘ List ⇒ GET
  8. Have any of you heard of the “Hypertext Application Language”?

    “I don’t understand the question and I won’t respond to it.”
  9. HAL = Hypertext Application Language Spec for hyperlinking resources Makes

    your API “explorable” “Simple” format to consume Uses _links & _embedded
  10. [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 ) ) ... )
  11. [id] => 6 [slug] => great-post ... [_links] => Array(...)

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

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

    [_embedded] => Array( [author] => Array( [0] => Array( [id] => 1 [name] => Jeremy Lindblom [slug] => jeremeamia ... [_links] => Array(...) ) ) ) “Simple” “Explorable”
  14. 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());
  15. 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’)
  16. 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
  17. 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
  18. Types of Authentication ➔ HTTP Basic Auth ➔ Requires Plugin

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

    ➔ Application-based ➔ USE HTTPS! ➔ Uses Admin’s credentials! ✘ Cookie Authentication ✘ OAuth Authentication ✘ Application Passwords ✘ Basic Auth
  20. 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’)
  21. 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
  22. $auth = base64_encode("{$user}:{$pass}"); $response = wp_remote_post( rest_url('wp/v2/posts/1'), [ 'method' =>

    'POST', 'headers' => [ 'Authorization' => "Basic {$auth}", ], 'body' => $data ] );
  23. 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.)