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

WordCamp Miami 2015

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers.

WordCamp Miami 2015

Avatar for Nikhil Vimal

Nikhil Vimal

May 31, 2015
Tweet

More Decks by Nikhil Vimal

Other Decks in Technology

Transcript

  1. Nikhil Vimal [
 {
 "goat_memes": {
 "i_goat_this": "http://gunnston.com/wp-content/uploads/2014/02/i-goat- this.jpg",
 "whatever_floats_your_goat":

    "http://i.redsnapperverytasty.com/graphics/ wp-content/uploads/2013/08/whatever-floats-your-goat.jpg",
 "row_your_goat": "http://wac.450f.edgecastcdn.net/80450F/92moose.fm/ files/2014/04/GoatMeme5.jpg",
 "here_we_goat_again":"http://treasure.diylol.com/uploads/post/image/ 334226/resized_screaming-goat-meme-generator-here-we-goat-again-e611e6.jpg",
 },
 
 "goat_gifs": {
 "goat_parkour":"http://i.imgur.com/4ETbG1R.gif"
 }
 }
 ]
  2. Nikhil Vimal $response = wp_remote_post( $url, array( 'method' => 'POST',

    'timeout' => 45, 'redirection' => 5, 'httpversion' => '1.0', 'blocking' => true, 'headers' => array(), 'body' => array( 'username' => 'bob', 'password' => '1234xyz' ), 'cookies' => array() ) ); if ( is_wp_error( $response ) ) { $error_message = $response->get_error_message(); echo "Something went wrong: $error_message"; } else { echo 'Response:<pre>'; print_r( $response ); echo '</pre>'; }
  3. Nikhil Vimal if (! $joke = get_transient('chuck_norris') ) { //

    If there's no cached version, let's get a joke $jsonurl = "http://api.icndb.com/jokes/random?limitTo=[nerdy]"; $json = wp_remote_get( $jsonurl ); if ( is_wp_error( $json ) ) { return "Chuck Norris accidentally kicked the server, it will be up soon!"; } else { // If everything's okay, parse the body and json_decode it $json_output = json_decode( wp_remote_retrieve_body( $json )); $joke = $json_output->value->joke; // Store the result in a transient, expires after 1 day // Also store it as the last successful using update_option if ($json_output->type = "success") { set_transient( 'chuck_norris', $joke, 60 * 1 ); } } } echo esc_html($joke); echo '<p><strong>Refresh Page for another great Chuck Norris Joke</strong></p>';
  4. Nikhil Vimal { "type": "success", "value": { "id": 449, "joke":

    "All arrays Chuck Norris declares are of infinite size, because Chuck Norris knows no bounds.", "categories": ["nerdy"] } }
  5. Nikhil Vimal { "type": "success", "value": { "id": 449, "joke":

    "All arrays Chuck Norris declares are of infinite size, because Chuck Norris knows no bounds.", "categories": ["nerdy"] } }