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.

1d2c0305ac4212ed2fb9c5d863513289?s=128

Jonathan Davis

November 03, 2012
Tweet

Transcript

  1. WordPress: Remote Controller Jonathan Davis, Ingenesis Limited @jonathandavis Friday, November

    2, 12
  2. Jonathan Davis Twitter: @jonathandavis Email: jon@shopplugin.net shopplugin.net Friday, November 2,

    12
  3. Friday, November 2, 12

  4. Friday, November 2, 12

  5. highest overhead fsockopen() Friday, November 2, 12

  6. sexy… but requires allow_url_fopen set Streams Friday, November 2, 12

  7. requires cURL extension cURL Friday, November 2, 12

  8. WP_Http one interface to rule them all image by VincentXyooj

    Friday, November 2, 12
  9. Request (educational use only.) Friday, November 2, 12

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

    array( 'code' => int, 'message' => string ), 'body' => string ); Friday, November 2, 12
  11. new WP_Http; doing_it_wrong()! Friday, November 2, 12

  12. 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
  13. 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
  14. Simple Twitter Search (yeah, it really works.) Friday, November 2,

    12
  15. Request (educational use only.) Friday, November 2, 12

  16. Simple Twitter Search (yeah, it really works.) Friday, November 2,

    12
  17. Basic Authentication Use WP_Http to authenticate for protected web services

    Friday, November 2, 12
  18. 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
  19. Web Services The Usual Suspects XML-RPC SOAP REST NVP Friday,

    November 2, 12
  20. From Here to There Copying a post from one WordPress

    site to another Friday, November 2, 12
  21. Turn on XML-RPC Under Settings -> Writing Friday, November 2,

    12
  22. Create an authorized user Be sure to set a role

    with proper access (Author) Friday, November 2, 12
  23. Friday, November 2, 12

  24. The Interface function post_remote_copy ( $post_id, $options = array() )

    { A post ID and some options Friday, November 2, 12
  25. 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
  26. 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
  27. 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
  28. 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
  29. 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
  30. 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
  31. 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
  32. 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
  33. Woot! No errors! error_log( __('Woohoo! Transport complete!') ); return true;

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

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

    => 'remotr', 'password' => 'c0ntro!!r' ) ); Friday, November 2, 12
  36. Oh noes! Error handling works though :) Friday, November 2,

    12
  37. Friday, November 2, 12

  38. hrm... post_remote_copy( 870, array( 'url' => 'http://website.com/xmlrpc.php', 'user' => 'remotr',

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

    'user' => 'remote', 'password' => 'c0ntro!!r' ) ); Friday, November 2, 12
  40. Awesome sauce. That’s what I’m talking about. Friday, November 2,

    12
  41. Magical. A new post out of nowhere! Friday, November 2,

    12
  42. 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
  43. Jonathan Davis Twitter: @jonathandavis Email: jon@shopplugin.net shopplugin.net Thank You! Friday,

    November 2, 12