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

(Not) Just Another WP REST API Talk

(Not) Just Another WP REST API Talk

What are REST APIs? History on the WP REST API, and some usage ideas & examples.

Kristen Symonds

March 12, 2017
Tweet

More Decks by Kristen Symonds

Other Decks in Technology

Transcript

  1. KRISTARELLA.blog (NOT) JUST ANOTHER WP REST API TALK OVERVIEW -

    What is a REST API? - What is the WP REST API? - A bit of history - Who is using it? - How to use it - What you can do with it - suggestions - examples
  2. KRISTARELLA.blog WHAT IS A REST API? DEFINITIONS - Application Programming

    Interface - method for an application to talk to another app/service/language - REpresentational State Transfer - enables access & manipulation of a textual representation of a web-based resource - coined by Roy Fielding in his 2000 dissertation
  3. KRISTARELLA.blog WHAT IS A REST API? CONSTRAINTS 1. Stateless
 Each

    request is independent of each other, and all information to complete the task needs to be included in one go 2. Cacheable 3. Uniform interface
 URI identifies the resource → method performs operation on resource → operation is implicit in the URL
 e.g., http://mybadass.blog/delete/product/coffee resource method
  4. KRISTARELLA.blog WHAT IS A REST API? CONSTRAINTS 4. Client-server
 A

    separation of client & server where the server doesn’t care about the state of the server and the client doesn’t need to store information 5. Layered
 Doesn’t matter if the server is primary, or an intermediary 6. Response is in a hypermedia format (optional) - XML is a hypermedia format because it conveys meaning within the markup - JSON is sometimes - API author needs to structure output in a RESTful way
  5. KRISTARELLA.blog IMMEDIATELY TURNS YOUR WEB SITE INTO AN APPLICATION THAT

    CAN SERVE DATA TO ANY KIND OF APPLICATION OR LANGUAGE Topher Derosia WHAT IS THE WP REST API?
  6. KRISTARELLA.blog WHAT IS THE WP REST API? 2009 MOMA NY

    uses WP back-end, Ruby front-end
 resulting in JSON API plugin Jan 2013 first code for WP REST API
 by Ryan McCue on Github Google SoC 2014 v1 stable: robust, but not ready for core
 (no internal API, difficult to extend) Feb 2015 started v2: Ryan McCue, Rachel Baker,
 Joe Hoyle, Daniel Bachhuber schemas posts, pages, media,
 terms, users controller, server Not to scale! Apr 2015 v2b1 May 2015 speed increase, custom
 response fields, pagination v2b2 Oct 2015 infrastructure merged into core! Jun 2013 Dec 2016 WP 4.7
 API merged into core!
  7. KRISTARELLA.blog - SearchWP - Tabulate - WooCommerce - EDD (not

    really) - ACF (with plugin)  WHO IS USING IT? PLUGINS
  8. KRISTARELLA.blog WHO IS USING IT? MOBILE - Official Mobile App

    - https://apps.wordpress.com/mobile/ - Vienna - https://github.com/joehoyle/vienna
  9. KRISTARELLA.blog HOW TO USE IT USING THE API 1. Find

    the URL you need - discovery URL is revealed in the head of your site 2. Construct the URL with resource and method 3. JSON reponse is returned 4. Parse information <link rel='https://api.w.org/' href='http://kristarella.blog/wp-json/' /> GET http://kristarella.blog/wp-json/wp/v2/posts
  10. KRISTARELLA.blog HOW TO USE IT EXAMPLE RESPONSE [{ “id" 4357,

    “date":"2015-03-02T22:27:02", “date_gmt":"2015-03-02T11:27:02", “guid":{"rendered":"http:\/\/www.kristarella.com\/?p=4357"}, “modified":"2015-03-03T10:09:40","modified_gmt":"2015-03-02T 23:09:40", “slug":"seo-for-beginners", “type":"post", “link":"http:\/\/kristarella.blog\/2015\/03\/seo-for- beginners\/", "title":{"rendered":"SEO for beginners"}, http://kristarella.blog/wp-json/wp/v2/posts?search=css
  11. KRISTARELLA.blog WHAT YOU CAN DO WITH IT IDEAS FOR YOU

    - On your site - suggest search terms for your visitors - pagination - archives/portfolio filter - replace admin-ajax.php tasks
 - Search a site from anywhere - streamline workflows - Quick post - Parse information into other APIs in any language - integrate signups with Salesforce, Meetup, Mailchimp - Display real-time sales data
  12. KRISTARELLA.blog WHAT YOU CAN DO WITH IT CUSTOM FRONTEND APP

    $http
 .get("http://ilovetea.mystagingwebsite.com/wp-json/wp/v2/posts? per_page=100&orderby=title")
 .success(function(data) { $scope.drinks = data; });
  13. KRISTARELLA.blog WHAT YOU CAN DO WITH IT SEARCH FROM ANYWHERE

    - Build your own search engine using Alfred workflows require './workflow.php';
 search( "{query}" );
  14. KRISTARELLA.blog WHAT YOU CAN DO WITH IT: SEARCH FROM ANYWHERE

    function search_v2( $search ) { $num = 30; $fields = ‘URL,title'; $order = ‘asc'; $orderby = 'title'; $url = 'https://public-api.wordpress.com/wp/v2/sites/en.support.wordpress.com/pages/?order='. $order.'&orderby='.$orderby.'&per_page='.$num.'&search='.urlencode( $search ); $ch = curl_init(); curl_setopt( $ch, CURLOPT_FAILONERROR, false ); curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true ); curl_setopt( $ch, CURLOPT_URL, $url ); curl_setopt( $ch, CURLOPT_TIMEOUT, 10 ); curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); curl_setopt($ch, CURLOPT_VERBOSE, true); $body = curl_exec( $ch ); $err = curl_errno( $ch ); $data = json_decode( $body, true ); if( !empty( $data ) ) { echo '<?xml version="1.0"?><items>'; while (list($key, $post) = each( $data ) ) { echo '<item uid="'.$key.'" arg="'.$post['link'].'"><title>'.$post['title']['rendered'].'</title> <subtitle>'.$post['link'].'</subtitle> </item>'; } echo "</items>"; } }