Integrating WordPress
With Web APIs
Randy Hoyt
About Me




            Randy Hoyt     randyhoyt.com
                           @randyhoyt


            Presentation   randyhoyt.com/wcmia


#wcmia                                     @randyhoyt
Overview



 • APIs: Defined
 • Two Examples:
         1. Get Treehouse Badges
         2. Get ShopLocket Products
 • WordPress Functions

#wcmia                                @randyhoyt
API
Application Programming Interface
A point where two systems,
  subjects, organizations, etc.,
  meet and interact.


Interface, http://dictionary.com/browse/interface
[Keyboard]
[Lock and Key]
An interface for software
 components to communicate
 with each other.


Wikipedia: API, http://trhou.se/defineAPI
API Examples




 • Android SDK: Camera API




#wcmia                       @randyhoyt
import android.hardware.Camera;


Camera.getNumberOfCameras();
Camera.open(cameraId);
API Examples




 • Android SDK: Camera API
 • WordPress: Plugin API


#wcmia                       @randyhoyt
<?php
/*
Plugin Name: Treehouse Badges
Description: Displays the badges ...
Version: 1.0
*/
?>


add_action('init','treehouse_badges_init');
...
API Examples




 • Android SDK: Camera API
 • WordPress: Plugin API
 • HTML5: Canvas API

#wcmia                       @randyhoyt
var canvas = document.getElementById("canvas");

var context = canvas.getContext("2d");

context.fillRect(10, 10, 40, 380, "#000000");

context.drawImage(img, x, y, 100, 77);
Web APIs
REST
Representational State Transfer
RESTful API
Example #1: Treehouse
[Treehouse screenshot]
JavaScript Object Notation:
 Text-based open standard
 designed for human-readable
 data interchange.


Wikipedia: JSON, http://trhou.se/defineJSON
{
     "firstName": "John",
     "lastName": "Smith",
     "address": {
         "streetAddress": "21 2nd Street",
         "city": "New York",
         "state": "NY",
         "postalCode": 10021
     },
     "phoneNumber": [
         {
             "type": "home",
             "number": "212 555-1234"
         },
         {
             "type": "fax",
             "number": "646 555-4567"
         }
     ]
 }

Wikipedia: JSON, http://trhou.se/defineJSON
[Treehouse JSON]
[HTML]
Asynchronous JavaScript and XML:
 Technique used by the browser to
 retrieve data from a server in the
 background without interfering with
 the existing page


Wikipedia: Ajax, http://trhou.se/defineAJAX
[AJAX Request Code]
[Badge Code]
[Badge Display]
<?php

$curl = curl_init();

curl_setopt_array($curl, array(

      CURLOPT_RETURNTRANSFER => 1,

      CURLOPT_URL => 'http://teamtreehou...

));

$resp = curl_exec($curl);

curl_close($curl);

?>
GET	
  Request	
  with	
  WordPress	
  HTTP	
  API




WordPress, http://codex.wordpress.org/HTTP_API
REST: Types of Requests



  • GET               (Read)
  • POST              (Create)
  • PUT               (Update)
  • DELETE            (Delete)

#wcmia                             @randyhoyt
API Questions




 • Does it have to
     be real time?

 • What if something
     goes wrong?

#wcmia                   @randyhoyt
Example #2: ShopLocket
[Screenshot of WP]
[Screenshot of WordPress]
[Screenshot of Product Picker]
[Screenshot of Product Picker]
[Screenshot of Shortcode]
[Screenshot of Website]
Authentication
OAuth
  An open protocol to allow secure
  authorization in a simple and standard
  method from web, mobile and desktop
  applications.




OAuth, http://oauth.net/
App ID
5f4a89f2d6655ac7a8859343d6d15257
9410e7b659b200e00aaf3721cf46269e


App Secret
fa963a1ef3702c16d934500f7621697e2
d6bfd05d3ef1751a32bdeb2a0599020
[ShopLocket]
[ShopLocket Login]
[ShopLocket]
/? code = 53fe... & state = auth...
$args = array(
    "code" => $_GET["code"],
    "app_id" => "app id",
    "app_secret" => "app secret",
);
           [GET Request]
$response = wp_remote_post(
               'https://www.shoplocket.com/'
                   . 'oauth/token',
               $args
            );
Token:
310826b20a535a422ccaa46d65c7065f
83e403b9218a38962ab4e43021b93585
[Token]
API Questions




 • Does it have to
     be real time?

 • What if something
     goes wrong?

#wcmia                   @randyhoyt
[Refresh Button]
WordPress Functions
WordPress wp_ajax, http://trhou.se/wp_ajax_hook
GET	
  Request	
  with	
  WordPress	
  HTTP	
  API




WordPress HTTP API, http://codex.wordpress.org/HTTP_API
WordPress update_option, http://trhou.se/update_option
Transients?




WordPress, http://codex.wordpress.org/Transients_API
Integrating WordPress
With Web APIs

Randy Hoyt     randyhoyt.com
               @randyhoyt

Presentation   randyhoyt.com/wcmia

Integrating WordPress With Web APIs