Upgrade to Pro — share decks privately, control downloads, hide ads and more …

APIness

 APIness

Strategies for using external APIs in WordPress, and how to make your website into an API itself.

Robert O'Rourke

April 26, 2014
Tweet

More Decks by Robert O'Rourke

Other Decks in Programming

Transcript

  1. APIs in WordPress ➔ Dashboard Widgets API ➔ Database API

    ➔ HTTP API ➔ File Header API ➔ Filesystem API ➔ Metadata API ➔ Options API ➔ Plugin API ➔ Quicktags API ➔ Rewrite API ➔ Settings API ➔ Shortcode API ➔ Theme Modification API ➔ Theme Customization API ➔ Transients API ➔ Widgets API ➔ XML-RPC WordPress API
  2. RSS

  3. $feed = fetch_feed( $uri ); // feed cache defaults to

    12 hours add_filter( 'wp_feed_cache_transient_lifetime', function( $seconds ) { return 60*60; // 1 hour } );
  4. 2 ways to extend oEmbed: // For sites that have

    an oEmbed endpoint wp_oembed_add_provider( $format, $provider, $regex ); // For everything else wp_embed_register_handler( $id, $regex, $callback, $priority ); function custom_embed( $matches, $attr, $url, $rawattr ) { // scrape target page, implement iframe embed etc... }
  5. Register handler for github gists: wp_embed_register_handler( ‘github-gists’, ‘#^(https?://gist.github.com/[^/]+/.+)$#i’, ‘github_embed’ );

    function github_embed( $matches, $attr, $url, $rawattr ) { return ‘<script src="’. $matches[1] .’.js"></script>’; }
  6. wp_remote_* // Performs a GET request wp_remote_get( $url, $args );

    // POST request wp_remote_post( $url, $args );
  7. wp_remote_* // use $args[ ‘body’ ] to POST // variables

    to $url wp_remote_get( $url, array( ‘body’ => array( ‘parameter’ => $value ) ) );
  8. // Fetch new auth keys for wp-config.php $url = ‘https://api.wordpress.org/secret-key/1.1/salt/’;

    $result = wp_remote_get( $url ); $keys = false; if ( ! is_wp_error( $result ) ) { // response contains information about the request if ( $result[ ‘response’ ][ ‘code’ ] === 200 ) $keys = $result[ ‘body’ ]; } // do something with $keys ¯\(°_o)/¯
  9. // Convert some markdown to HTML using Github $url =

    ‘https://api.github.com/markdown’; $result = wp_remote_post( $url, array( ‘body’ => array( ‘text’ => ‘# Air Hair Lair! #’ ) ) ); echo $result[ ‘body’ ]; // <h1>Air Hair Lair!</h1>
  10. $text = $_POST[ ‘markdown’ ]; $option_key = ‘markdown_’ . md5(

    $text ); // Check if we have the conversion already // using a hash as the key if ( false === ( $markdown = get_option( $option_key ) ) ) { $result = wp_remote_post( $url, ... ); $markdown = $result[ ‘body’ ]; update_option( $option_key, $markdown ); } echo $markdown;
  11. $text = $_POST[ ‘markdown’ ]; $transient_key = ‘markdown_’ . md5(

    $text ); // Check if we have the conversion already // using a hash as the key if ( false === ( $rsp = get_transient( $transient_key ) ) ) { $result = wp_remote_post( $url, ... ); $rsp = $result[ ‘body’ ]; set_transient( $transient_key, $rsp, 60*60 ); // 1 hour } echo $rsp;
  12. add_action( ‘trigger_api’, ‘do_api_call’, 10, 2 ); function do_api_call( $url, $args

    ) { // fetch / send data & cache response } function api_call( $url, $args ) { $key = md5( $url . serialize( $args ) ); if ( false === ( $data = get_transient( $key ) ) ) { wp_schedule_single_event( time(), ‘trigger_api’, array( ‘url’ => $url, ‘args’ => $args ) ); } return $data; }
  13. headers are useful $result = wp_remote_post( $url ); // number

    of requests per rate limit window $result[ ‘headers’ ][ ‘X-Rate-Limit-Limit’ ]; // number of requests you have left $result[ ‘headers’ ][ ‘X-Rate-Limit-Remaining’ ]; // the unix time when the limit remaining resets $result[ ‘headers’ ][ ‘X-Rate-Limit-Reset’ ];
  14. $limits = get_option( ‘ratelimit_’ . md5( $url ) ); if

    ( false === ( $data = get_option( $key ) ) && $limits && $limits[‘remaining’] > 0 ) { $result = wp_remote_post( $url ); $limits = array( ‘remaining’ => $result[‘headers’][‘X-Rate-Limit-Remaining’], ‘reset’ => $result[‘headers’][‘X-Rate-Limit-Reset’] ); update_option( ‘ratelimit_’ . md5( $url ), $limits ); update_option( $key, $result[ ‘body’ ] ); }
  15. tmhOAuth $api = new tmhOAuth( array( ‘host’ => ‘api.twitter.com’ 'consumer_key'

    => '', 'consumer_secret' => '', 'token' => '', 'secret' => '', ) ); $data = $api->request( ‘POST’, $url, $params );
  16. in practice // you can make requests to URLs like

    the following /wp-json/posts/<id> // the above supports the following methods: // "HEAD", // "GET", returns an entity // "POST", adds a new entity // "PUT", updates an entity // "DELETE" deletes the entity