integrating wordpress with web apis

84
Integrating WordPress With Web APIs Randy Hoyt

Upload: randyhoyt

Post on 12-May-2015

4.307 views

Category:

Technology


0 download

DESCRIPTION

An application programming interface (API) is a way for two different pieces of software to communicate with each other. In your WordPress plugins and themes, you’ll often want to pull data from or send data to a third-party service that has an API. In this talk, Randy will explain the terminology you need to know to get started, share best practices and techniques for integrating with APIs, and walk through two real-world examples. You’ll leave with code snippets to help you get started integrating.

TRANSCRIPT

Page 1: Integrating WordPress With Web APIs

Integrating WordPressWith Web APIs

Randy Hoyt

Page 2: Integrating WordPress With Web APIs

#wcmia @randyhoyt@randyhoyt

Randy Hoyt randyhoyt.com

@randyhoyt

Presentation randyhoyt.com/wcmia

About Me

Page 3: Integrating WordPress With Web APIs

#wcmia @randyhoyt@randyhoyt

• APIs: Defined

• Two Examples:

1. Get Treehouse Badges

2. Get ShopLocket Products

• WordPress Functions

Overview

Page 4: Integrating WordPress With Web APIs

API

Page 5: Integrating WordPress With Web APIs

Application Programming Interface

Page 6: Integrating WordPress With Web APIs

A point where two systems, subjects, organizations, etc., meet and interact.

Interface, http://dictionary.com/browse/interface

Page 7: Integrating WordPress With Web APIs

[Keyboard]

Page 8: Integrating WordPress With Web APIs

[Lock and Key]

Page 9: Integrating WordPress With Web APIs

An interface for software components to communicate with each other.

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

Page 10: Integrating WordPress With Web APIs

#wcmia @randyhoyt@randyhoyt

• Android SDK: Camera API

API Examples

Page 11: Integrating WordPress With Web APIs

import android.hardware.Camera;

Camera.getNumberOfCameras();

Camera.open(cameraId);

Page 12: Integrating WordPress With Web APIs

#wcmia @randyhoyt@randyhoyt

• Android SDK: Camera API

• WordPress: Plugin API

API Examples

Page 13: Integrating WordPress With Web APIs

<?php

/*

Plugin Name: Treehouse Badges

Description: Displays the badges ...

Version: 1.0

*/

?>

add_action('init','treehouse_badges_init');

...

Page 14: Integrating WordPress With Web APIs

#wcmia @randyhoyt@randyhoyt

• Android SDK: Camera API

• WordPress: Plugin API

• HTML5: Canvas API

API Examples

Page 15: Integrating WordPress With Web APIs

var canvas = document.getElementById("canvas");

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

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

context.drawImage(img, x, y, 100, 77);

Page 16: Integrating WordPress With Web APIs

Web APIs

Page 17: Integrating WordPress With Web APIs

REST

Page 18: Integrating WordPress With Web APIs

Representational State Transfer

Page 19: Integrating WordPress With Web APIs

RESTful API

Page 20: Integrating WordPress With Web APIs

Example #1: Treehouse

Page 21: Integrating WordPress With Web APIs

[Treehouse screenshot]

Page 22: Integrating WordPress With Web APIs

JavaScript Object Notation:Text-based open standard designed for human-readable data interchange.

Wikipedia: JSON, http://trhou.se/defineJSON

Page 23: Integrating WordPress With Web APIs

{ "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

Page 24: Integrating WordPress With Web APIs

[Treehouse JSON]

Page 25: Integrating WordPress With Web APIs

[HTML]

Page 26: Integrating WordPress With Web APIs

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

Page 27: Integrating WordPress With Web APIs

[AJAX Request Code]

Page 28: Integrating WordPress With Web APIs

[Badge Code]

Page 29: Integrating WordPress With Web APIs

[Badge Display]

Page 30: Integrating WordPress With Web APIs
Page 31: Integrating WordPress With Web APIs
Page 32: Integrating WordPress With Web APIs
Page 33: Integrating WordPress With Web APIs
Page 34: Integrating WordPress With Web APIs
Page 35: Integrating WordPress With Web APIs
Page 36: Integrating WordPress With Web APIs
Page 37: Integrating WordPress With Web APIs
Page 38: Integrating WordPress With Web APIs

<?php

$curl = curl_init();

curl_setopt_array($curl, array(

CURLOPT_RETURNTRANSFER => 1,

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

));

$resp = curl_exec($curl);

curl_close($curl);

?>

Page 39: Integrating WordPress With Web APIs

GET  Request  with  WordPress  HTTP  API

WordPress, http://codex.wordpress.org/HTTP_API

Page 40: Integrating WordPress With Web APIs

#wcmia @randyhoyt@randyhoyt

• GET

• POST

• PUT

• DELETE

REST: Types of Requests

(Read)

(Create)

(Update)

(Delete)

Page 41: Integrating WordPress With Web APIs
Page 42: Integrating WordPress With Web APIs
Page 43: Integrating WordPress With Web APIs
Page 44: Integrating WordPress With Web APIs
Page 45: Integrating WordPress With Web APIs
Page 46: Integrating WordPress With Web APIs

#wcmia @randyhoyt@randyhoyt

• Does it have to be real time?

• What if something goes wrong?

API Questions

Page 47: Integrating WordPress With Web APIs

Example #2: ShopLocket

Page 48: Integrating WordPress With Web APIs

[Screenshot of WP]

Page 49: Integrating WordPress With Web APIs

[Screenshot of WordPress]

Page 50: Integrating WordPress With Web APIs

[Screenshot of Product Picker]

Page 51: Integrating WordPress With Web APIs

[Screenshot of Product Picker]

Page 52: Integrating WordPress With Web APIs

[Screenshot of Shortcode]

Page 53: Integrating WordPress With Web APIs

[Screenshot of Website]

Page 54: Integrating WordPress With Web APIs

Authentication

Page 55: Integrating WordPress With Web APIs

OAuthAn open protocol to allow secure authorization in a simple and standard method from web, mobile and desktop applications.

OAuth, http://oauth.net/

Page 56: Integrating WordPress With Web APIs

App ID

5f4a89f2d6655ac7a8859343d6d152579410e7b659b200e00aaf3721cf46269e

App Secret

fa963a1ef3702c16d934500f7621697e2d6bfd05d3ef1751a32bdeb2a0599020

Page 57: Integrating WordPress With Web APIs

[ShopLocket]

Page 58: Integrating WordPress With Web APIs

[ShopLocket Login]

Page 59: Integrating WordPress With Web APIs

[ShopLocket]

Page 60: Integrating WordPress With Web APIs

/? code = 53fe... & state = auth...

Page 61: Integrating WordPress With Web APIs

[GET Request]

$args = array( "code" => $_GET["code"], "app_id" => "app id", "app_secret" => "app secret",);

$response = wp_remote_post( 'https://www.shoplocket.com/' . 'oauth/token', $args );

Page 62: Integrating WordPress With Web APIs

Token:

310826b20a535a422ccaa46d65c7065f83e403b9218a38962ab4e43021b93585

Page 63: Integrating WordPress With Web APIs

[Token]

Page 64: Integrating WordPress With Web APIs

#wcmia @randyhoyt@randyhoyt

• Does it have to be real time?

• What if something goes wrong?

API Questions

Page 65: Integrating WordPress With Web APIs
Page 66: Integrating WordPress With Web APIs
Page 67: Integrating WordPress With Web APIs
Page 68: Integrating WordPress With Web APIs
Page 69: Integrating WordPress With Web APIs
Page 70: Integrating WordPress With Web APIs
Page 71: Integrating WordPress With Web APIs
Page 72: Integrating WordPress With Web APIs
Page 73: Integrating WordPress With Web APIs

[Refresh Button]

Page 74: Integrating WordPress With Web APIs
Page 75: Integrating WordPress With Web APIs

WordPress Functions

Page 76: Integrating WordPress With Web APIs
Page 77: Integrating WordPress With Web APIs
Page 78: Integrating WordPress With Web APIs

WordPress wp_ajax, http://trhou.se/wp_ajax_hook

Page 79: Integrating WordPress With Web APIs
Page 80: Integrating WordPress With Web APIs

GET  Request  with  WordPress  HTTP  API

WordPress HTTP API, http://codex.wordpress.org/HTTP_API

Page 81: Integrating WordPress With Web APIs
Page 82: Integrating WordPress With Web APIs

WordPress update_option, http://trhou.se/update_option

Page 83: Integrating WordPress With Web APIs

Transients?

WordPress, http://codex.wordpress.org/Transients_API

Page 84: Integrating WordPress With Web APIs

Integrating WordPressWith Web APIs

Randy Hoyt randyhoyt.com @randyhoyt

Presentation randyhoyt.com/wcmia