integrating external apis with wordpress

21
Integrating External APIs

Upload: marty-thornley

Post on 16-Jul-2015

379 views

Category:

Internet


2 download

TRANSCRIPT

Page 1: Integrating External APIs with WordPress

Integrating External APIs

Page 2: Integrating External APIs with WordPress

Freelance WordPress Developer 8+ years

Owner and Lead Developer at

PhotographyBlogSites.com

martythornley.com @martythornley

A Little About Me

Page 3: Integrating External APIs with WordPress

Application Program Interface

Allows programs

to communicate

with each other

What is an API?

Get information

Send information

Page 4: Integrating External APIs with WordPress

What do APIs do?

TwitterDisplay Tweets

YelpDisplay Listings &

Search

AmazonList & Sell

Products

Your Website or App

Page 5: Integrating External APIs with WordPress

What do APIs do?

TwitterPost Tweets

Your Website or App

FacebookSend new status,

images, location

InstagramCreate Likes

Page 6: Integrating External APIs with WordPress

https://codex.wordpress.org/WordPress_APIs

Internal WordPress APIs

Makes plugins possible

Makes talking to the database easier

Makes theme customization possible

Makes communicating with external API’s easier

Page 7: Integrating External APIs with WordPress

HTTP API

Database API

Options API

Transients API

Metadata API

Plugin API

https://codex.wordpress.org/WordPress_APIs

Internal WordPress APIs

Dashboard Widgets API

File Header API

Filesystem API

Quicktags API

Rewrite API

Shortcode API

Theme Modification API

Theme Customization API

Widgets API

XML-RPC WordPress API

Settings API

Page 8: Integrating External APIs with WordPress

How To Use APIs

Endpoint

Authentication

Methods

Arguments

Response

URL where API ‘lives’

May be public or require authentication

Possible ways of using the API

Specify what you want it to do

What it sends back

Page 9: Integrating External APIs with WordPress

Authenticate with APIs

None

Basic

OAuth

Not typical

Encoded username:password combo

or an API KEY

Complicated

Page 10: Integrating External APIs with WordPress

WordPress HTTP API

wp_remote_get

wp_remote_post

Retrieve URL using GET HTTP method

Retrieve URL using POST HTTP method

https://codex.wordpress.org/HTTP_API

$response = wp_remote_get( $url , $args );

Page 11: Integrating External APIs with WordPress

Response from an API

Array

(

[headers] => Array

(

[content-type] => text/html; charset=utf-8

[date] => Wed, 25 Mar 2015 03:43:43 GMT

[server] => Mashape/5.0.6

[via] => 1.1 vegur

[x-powered-by] => Express

[content-length] => 51

[connection] => Close

)

[body] => THE STUFF WE WANT

[response] => Array

(

[code] => 200

[message] => OK

)

[cookies] => Array

(

)

[filename] =>

)

Page 12: Integrating External APIs with WordPress

Response from an API

Array

(

[headers] => Array

(

[content-type] => text/html; charset=utf-8

[date] => Wed, 25 Mar 2015 03:43:43 GMT

[server] => Mashape/5.0.6

[via] => 1.1 vegur

[x-powered-by] => Express

[content-length] => 51

[connection] => Close

)

[body] => THE STUFF WE WANT

[response] => Array

(

[code] => 200

[message] => OK

)

[cookies] => Array

(

)

[filename] =>

)

Page 13: Integrating External APIs with WordPress

WordPress HTTP API

https://codex.wordpress.org/HTTP_API

$headers = wp_remote_retrieve_headers( $response );

$headers = wp_remote_retrieve_header( $response );

$code = wp_remote_retrieve_response_code( $response );

$message = wp_remote_retrieve_response_message( $response );

$body = wp_remote_retrieve_body( $response );

Page 14: Integrating External APIs with WordPress

Error Checking Response

$response = wp_remote_get( $url , $args );

$code = wp_remote_retrieve_code( $response );

if ( $code == ‘200’ ) {

$body = wp_remote_retrieve_body( $response );

}

Page 15: Integrating External APIs with WordPress

What Format is Response?

string

xml

json

$array = json_decode( $body );

$json = json_encode( $array );

Page 16: Integrating External APIs with WordPress

Caching Response

Play nice with the API / Save your rate limits

Don’t wait on external services when possible

Use WordPress API’s to help

Options

Transients

WP_Object_Cache

Meta

Save to db

Save to db with time limit

Cache in memory

User / Post Meta

Page 17: Integrating External APIs with WordPress

Caching Response

set_transient( ‘my_transient_name’ , $body , 30 );

get_transient( ‘my_transient_name’ );

delete_transient( ‘my_transient_name’ );

set_site_transient( ‘my_transient_name’ , $body , 30 );

get_site_transient( ‘my_transient_name’ );

delete_site_transient( ‘my_transient_name’ );

https://codex.wordpress.org/Transients_API

Page 18: Integrating External APIs with WordPress

Caching Response

$body = get_transient( ‘my_transient_name’ );

if ( $body !== false ) {

$response = wp_remote_get( $url , $args );

$code = wp_remote_retrieve_code( $response );

if ( $code == ‘200’ ) {

$body = wp_remote_retrieve_body( $response );

set_transient( ‘my_transient_name’ , $body , 30 );

}

}

Page 19: Integrating External APIs with WordPress

Example API Plugin

Yoda Speak

http://github.org/martythornley/yoda-speak

Page 20: Integrating External APIs with WordPress

Where to find APIs

http://www.programmableweb.com/

https://www.mashape.com

http://apis.io/

Page 21: Integrating External APIs with WordPress

Questions

[email protected]

martythornley.com

photographyblogsites.com