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
    WP REST API
    (NOT) JUST ANOTHER … TALK

    View Slide

  2. KRISTARELLA.blog
    @KRISTARELLA

    HAPPINESS ENGINEER: WP.COM
    KRISTEN SYMONDS

    View Slide

  3. 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

    View Slide

  4. KRISTARELLA.blog
    REST API?
    WHAT IS A…

    View Slide

  5. 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

    View Slide

  6. 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

    View Slide

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

    View Slide

  8. KRISTARELLA.blog
    WP REST API?
    WHAT IS THE…

    View Slide

  9. 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?

    View Slide

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

    View Slide

  11. KRISTARELLA.blog
    WHO IS USING IT?
    WP REST API

    View Slide

  12. KRISTARELLA.blog
    - SearchWP
    - Tabulate
    - WooCommerce
    - EDD (not really)
    - ACF (with plugin)

    WHO IS USING IT?
    PLUGINS

    View Slide

  13. KRISTARELLA.blog
    SITES
    WHO IS USING IT?

    View Slide

  14. KRISTARELLA.blog
    ADMIN
    WHO IS USING IT?

    View Slide

  15. KRISTARELLA.blog
    WHO IS USING IT?
    MOBILE
    - Official Mobile App
    - https://apps.wordpress.com/mobile/
    - Vienna
    - https://github.com/joehoyle/vienna

    View Slide

  16. KRISTARELLA.blog
    HOW TO USE IT
    WP REST API

    View Slide

  17. KRISTARELLA.blog
    WP-API.ORG
    DEVELOPER.WORDPRESS.ORG/REST-API

    View Slide

  18. 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

    GET http://kristarella.blog/wp-json/wp/v2/posts

    View Slide

  19. 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

    View Slide

  20. 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

    View Slide

  21. 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;
    });

    View Slide

  22. KRISTARELLA.blog

    View Slide

  23. KRISTARELLA.blog
    WHAT YOU CAN DO WITH IT
    SEARCH FROM ANYWHERE
    - Build your own search engine using Alfred
    workflows
    require './workflow.php';

    search( "{query}" );

    View Slide

  24. 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 '';
    while (list($key, $post) = each( $data ) ) {
    echo ''.$post['title']['rendered'].'
    '.$post['link'].'
    ';
    }
    echo "";
    }
    }

    View Slide

  25. KRISTARELLA.blog
    CREDITS
    - Images
    - https://www.flickr.com/photos/
    swamibu/6818776091
    - https://www.flickr.com/photos/
    peter-trimming/15030180387

    View Slide

  26. QUESTIONS?
    THANK YOU!
    KRISTARELLA.blog

    View Slide