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

WordPress: Remote Controller

WordPress: Remote Controller

A primer on how to use the HTTP API in WordPress for remote communication. Learn best practices, review different web service approaches, and watch a practical example copy a post from one WordPress install to another using XML-RPC.

Jonathan Davis

November 03, 2012

More Decks by Jonathan Davis

Other Decks in Programming


  1. The Response $response = array( 'headers' => array(), 'response' =>

    array( 'code' => int, 'message' => string ), 'body' => string ); Friday, November 2, 12
  2. Use the Wrappers wp-includes/http.php wp_remote_request() Retrieve the raw response from

    the HTTP request. wp_remote_get() Retrieve the raw response from the HTTP request using the GET method. wp_remote_post() Retrieve the raw response from the HTTP request using the POST method. wp_remote_head() Retrieve the raw response from the HTTP request using the HEAD method. Friday, November 2, 12
  3. Helpful Functions wp-includes/http.php wp_remote_retrieve_headers() Retrieve only the headers from the

    raw response. wp_remote_retrieve_response_code() Retrieve the HTTP stats response code from the raw response. wp_remote_retrieve_response_messsage() Retrieve the HTTP status response message from the raw response. wp_remote_retrieve_body() Retrieve only the body from the raw response. Friday, November 2, 12
  4. Basic Authentication $credentials = base64_encode( "$username:$password" ); $headers = array(

    'Authorization' => "Basic $credentials" ); $response = wp_remote_get( $url, array( 'headers' => $headers ) ); base64_encode FTW wp_remote_post() works too Friday, November 2, 12
  5. From Here to There Copying a post from one WordPress

    site to another Friday, November 2, 12
  6. Create an authorized user Be sure to set a role

    with proper access (Author) Friday, November 2, 12
  7. The Interface function post_remote_copy ( $post_id, $options = array() )

    { A post ID and some options Friday, November 2, 12
  8. Setup the options $options = wp_parse_args( $options, array( 'url' =>

    'http://website.com/xmlrpc.php', 'blogid' => 0, 'user' => 'username', 'password' => 'passsword', 'errorlog' => __('Error posting remotely: %s') ) ); extract($options,EXTR_SKIP); The endpoint URL, blog id, remote user name, password Then extract the options to local variables ($url, $user, etc.) Friday, November 2, 12
  9. Get the post from WordPress $post = get_post($post_id); Pass the

    $post_id from our function argument Get the post stdClass back in $post Friday, November 2, 12
  10. Make sure the post exists if ( is_null($post) ) {

    error_log( sprintf($errorlog,__("$post_id was not found.")) ); return false; } Log an error, and bail with a boolean result of false Friday, November 2, 12
  11. Copy the post data $content = array( 'post_type' => $post->post_type,

    'post_title' => "{$post->post_title} Copy", 'post_content' => $post->post_content, 'post_status' => 'draft', 'comment_status' => $post->comment_status, 'ping_status' => $post->ping_status, ); Duplicate the data for the $content argument of the request Friday, November 2, 12
  12. Format the request $request = xmlrpc_encode_request( 'wp.newPost', array( $blogid, $user,

    $pwd, $content ) ); Pack the request using xmlrpc_encode_request method, blog id, user name, password, the post content Friday, November 2, 12
  13. Hailing frequencies open... $result = wp_remote_post( $url, array('body' => $request)

    ); Send the xml encoded request in the body parameter Get the result back Friday, November 2, 12
  14. Parse the response $body = wp_remote_retrieve_body($result); $XML = simplexml_load_string($body); Grab

    the body from the result Parse the XML data into a structured object Friday, November 2, 12
  15. Real programmers handle errors. if ( '200' != wp_remote_retrieve_response_code($result) )

    { $message = wp_remote_retrieve_response_message($result); error_log( sprintf($errorlog,$message) ); return false; } elseif ( isset($XML->fault) ) { $message = $XML->fault->value->struct->member[1]->value->string; error_log( sprintf($errorlog,$message) ); return false; } Check for HTTP communication errors Log any errors and bail out by returning false Friday, November 2, 12
  16. Woot! No errors! error_log( __('Woohoo! Transport complete!') ); return true;

    Log how awesome we are. Send back boolean true Friday, November 2, 12
  17. Ready to go. If it works, we’ll see a new

    entry: Code is Poetry Copy - Draft Friday, November 2, 12
  18. Let’s do this! post_remote_copy( 870, array( 'url' => 'http://website.com/xmlrpc.php', 'user'

    => 'remotr', 'password' => 'c0ntro!!r' ) ); Friday, November 2, 12
  19. Here we go again... post_remote_copy( 870, array( 'url' => 'http://website.com/xmlrpc.php',

    'user' => 'remote', 'password' => 'c0ntro!!r' ) ); Friday, November 2, 12
  20. References HTTP API http://codex.wordpress.org/HTTP_API How to Make HTTP Requests with

    WordPress http://planetozh.com/blog/2009/08/how-to-make-http-requests-with-wordpress/ A Note on WP_Http and HTTP Requests in WordPress http://kovshenin.com/2011/a-note-on-wp_http-and-http-requests-in-wordpress/ WP_Http class wp-includes/class-http.php WP_Http functions wp-includes/http.php Friday, November 2, 12