Slide 1

Slide 1 text

WordPress: Remote Controller Jonathan Davis, Ingenesis Limited @jonathandavis Friday, November 2, 12

Slide 2

Slide 2 text

Jonathan Davis Twitter: @jonathandavis Email: [email protected] shopplugin.net Friday, November 2, 12

Slide 3

Slide 3 text

Friday, November 2, 12

Slide 4

Slide 4 text

Friday, November 2, 12

Slide 5

Slide 5 text

highest overhead fsockopen() Friday, November 2, 12

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

requires cURL extension cURL Friday, November 2, 12

Slide 8

Slide 8 text

WP_Http one interface to rule them all image by VincentXyooj Friday, November 2, 12

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

The Response $response = array( 'headers' => array(), 'response' => array( 'code' => int, 'message' => string ), 'body' => string ); Friday, November 2, 12

Slide 11

Slide 11 text

new WP_Http; doing_it_wrong()! Friday, November 2, 12

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

Basic Authentication Use WP_Http to authenticate for protected web services Friday, November 2, 12

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

Web Services The Usual Suspects XML-RPC SOAP REST NVP Friday, November 2, 12

Slide 20

Slide 20 text

From Here to There Copying a post from one WordPress site to another Friday, November 2, 12

Slide 21

Slide 21 text

Turn on XML-RPC Under Settings -> Writing Friday, November 2, 12

Slide 22

Slide 22 text

Create an authorized user Be sure to set a role with proper access (Author) Friday, November 2, 12

Slide 23

Slide 23 text

Friday, November 2, 12

Slide 24

Slide 24 text

The Interface function post_remote_copy ( $post_id, $options = array() ) { A post ID and some options Friday, November 2, 12

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

Woot! No errors! error_log( __('Woohoo! Transport complete!') ); return true; Log how awesome we are. Send back boolean true Friday, November 2, 12

Slide 34

Slide 34 text

Ready to go. If it works, we’ll see a new entry: Code is Poetry Copy - Draft Friday, November 2, 12

Slide 35

Slide 35 text

Let’s do this! post_remote_copy( 870, array( 'url' => 'http://website.com/xmlrpc.php', 'user' => 'remotr', 'password' => 'c0ntro!!r' ) ); Friday, November 2, 12

Slide 36

Slide 36 text

Oh noes! Error handling works though :) Friday, November 2, 12

Slide 37

Slide 37 text

Friday, November 2, 12

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

Awesome sauce. That’s what I’m talking about. Friday, November 2, 12

Slide 41

Slide 41 text

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

Slide 42

Slide 42 text

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

Slide 43

Slide 43 text

Jonathan Davis Twitter: @jonathandavis Email: [email protected] shopplugin.net Thank You! Friday, November 2, 12