Integrating External APIs with WordPress
-
Upload
marty-thornley -
Category
Internet
-
view
379 -
download
2
Transcript of Integrating External APIs with WordPress
![Page 1: Integrating External APIs with WordPress](https://reader033.fdocuments.in/reader033/viewer/2022042701/55a7994e1a28ab33628b4645/html5/thumbnails/1.jpg)
Integrating External APIs
![Page 2: Integrating External APIs with WordPress](https://reader033.fdocuments.in/reader033/viewer/2022042701/55a7994e1a28ab33628b4645/html5/thumbnails/2.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022042701/55a7994e1a28ab33628b4645/html5/thumbnails/3.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022042701/55a7994e1a28ab33628b4645/html5/thumbnails/4.jpg)
What do APIs do?
TwitterDisplay Tweets
YelpDisplay Listings &
Search
AmazonList & Sell
Products
Your Website or App
![Page 5: Integrating External APIs with WordPress](https://reader033.fdocuments.in/reader033/viewer/2022042701/55a7994e1a28ab33628b4645/html5/thumbnails/5.jpg)
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://reader033.fdocuments.in/reader033/viewer/2022042701/55a7994e1a28ab33628b4645/html5/thumbnails/6.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022042701/55a7994e1a28ab33628b4645/html5/thumbnails/7.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022042701/55a7994e1a28ab33628b4645/html5/thumbnails/8.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022042701/55a7994e1a28ab33628b4645/html5/thumbnails/9.jpg)
Authenticate with APIs
None
Basic
OAuth
Not typical
Encoded username:password combo
or an API KEY
Complicated
![Page 10: Integrating External APIs with WordPress](https://reader033.fdocuments.in/reader033/viewer/2022042701/55a7994e1a28ab33628b4645/html5/thumbnails/10.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022042701/55a7994e1a28ab33628b4645/html5/thumbnails/11.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022042701/55a7994e1a28ab33628b4645/html5/thumbnails/12.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022042701/55a7994e1a28ab33628b4645/html5/thumbnails/13.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022042701/55a7994e1a28ab33628b4645/html5/thumbnails/14.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022042701/55a7994e1a28ab33628b4645/html5/thumbnails/15.jpg)
What Format is Response?
string
xml
json
$array = json_decode( $body );
$json = json_encode( $array );
![Page 16: Integrating External APIs with WordPress](https://reader033.fdocuments.in/reader033/viewer/2022042701/55a7994e1a28ab33628b4645/html5/thumbnails/16.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022042701/55a7994e1a28ab33628b4645/html5/thumbnails/17.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022042701/55a7994e1a28ab33628b4645/html5/thumbnails/18.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022042701/55a7994e1a28ab33628b4645/html5/thumbnails/19.jpg)
Example API Plugin
Yoda Speak
http://github.org/martythornley/yoda-speak
![Page 20: Integrating External APIs with WordPress](https://reader033.fdocuments.in/reader033/viewer/2022042701/55a7994e1a28ab33628b4645/html5/thumbnails/20.jpg)
Where to find APIs
http://www.programmableweb.com/
https://www.mashape.com
http://apis.io/