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 Yelp Display Listings & Search Amazon List & Sell Products Your Website or App
What do APIs do? TwitterPost Tweets Your Website or App Facebook Send 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/
Questions martythornley@gmail.com martythornley.com photographyblogsites.com

Integrating External APIs with WordPress

  • 1.
  • 2.
    Freelance WordPress Developer8+ years Owner and Lead Developer at PhotographyBlogSites.com martythornley.com @martythornley A Little About Me
  • 3.
    Application Program Interface Allowsprograms to communicate with each other What is an API? Get information Send information
  • 4.
    What do APIsdo? TwitterDisplay Tweets Yelp Display Listings & Search Amazon List & Sell Products Your Website or App
  • 5.
    What do APIsdo? TwitterPost Tweets Your Website or App Facebook Send new status, images, location InstagramCreate Likes
  • 6.
    https://codex.wordpress.org/WordPress_APIs Internal WordPress APIs Makesplugins possible Makes talking to the database easier Makes theme customization possible Makes communicating with external API’s easier
  • 7.
    HTTP API Database API OptionsAPI 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
  • 8.
    How To UseAPIs 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
  • 9.
    Authenticate with APIs None Basic OAuth Nottypical Encoded username:password combo or an API KEY Complicated
  • 10.
    WordPress HTTP API wp_remote_get wp_remote_post RetrieveURL using GET HTTP method Retrieve URL using POST HTTP method https://codex.wordpress.org/HTTP_API $response = wp_remote_get( $url , $args );
  • 11.
    Response from anAPI 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] => )
  • 12.
    Response from anAPI 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] => )
  • 13.
    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 );
  • 14.
    Error Checking Response $response= wp_remote_get( $url , $args ); $code = wp_remote_retrieve_code( $response ); if ( $code == ‘200’ ) { $body = wp_remote_retrieve_body( $response ); }
  • 15.
    What Format isResponse? string xml json $array = json_decode( $body ); $json = json_encode( $array );
  • 16.
    Caching Response Play nicewith 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
  • 17.
    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
  • 18.
    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 ); } }
  • 19.
    Example API Plugin YodaSpeak http://github.org/martythornley/yoda-speak
  • 20.
    Where to findAPIs http://www.programmableweb.com/ https://www.mashape.com http://apis.io/
  • 21.