Integrating External APIs
Integrating External APIs
Freelance WordPress Developer 8+ years
Owner and Lead Developer at
PhotographyBlogSites.com
martythornley.com @martythornley
A Little About Me
Application Program Interface
Allows programs
to communicate
with each other
What is an API?
Get information
Send information
What do APIs do?
TwitterDisplay Tweets
YelpDisplay Listings &
Search
AmazonList & Sell
Products
Your Website or App
What do APIs do?
TwitterPost Tweets
Your Website or App
FacebookSend new status,
images, location
InstagramCreate Likes
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
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
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
Authenticate with APIs
None
Basic
OAuth
Not typical
Encoded username:password combo
or an API KEY
Complicated
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 );
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] =>
)
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] =>
)
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 );
Error Checking Response
$response = wp_remote_get( $url , $args );
$code = wp_remote_retrieve_code( $response );
if ( $code == ‘200’ ) {
$body = wp_remote_retrieve_body( $response );
}
What Format is Response?
string
xml
json
$array = json_decode( $body );
$json = json_encode( $array );
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
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
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 );
}
}
Example API Plugin
Yoda Speak
http://github.org/martythornley/yoda-speak
Where to find APIs
http://www.programmableweb.com/
https://www.mashape.com
http://apis.io/