I use and love the WordPress platform for this and several other blogs that I maintain. One of the many cool features of WordPress is that it supports XML-RPC. What does that mean? Well XML is… XML. It’s the extensible markup language and is basically just a way of formatting data in some meaningful way. RPC stands for remote procedure call. That is making calls to methods/procedures on a remote system. Put them together and you get the idea… making method/procedure calls on remote systems using XML to bundle up the method calls and returns.
Great,
I actually decided not to use any of the APIs mentioned above. Instead I went with the Redstone XML-RPC library. This API provides a layer of abstraction from all of the XML-RPC details that is nice and it also gives me the flexibility to implement any methods that I want. This will come in handy as you will see in a later post. For now though, how might I make a post using this API? Simple really. We’ll start out with the Blogger API method call. The method signature looks like this:
blogger.newPost(blog_ID, user_login, user_pass, content, publish)
The blog_ID is used for systems that maintain multiple blogs. There is actually a method we could call to get that piece of information called blogger.getUsersBlogs
. Here’s a little secret though… WordPress doesn’t currently support this feature and is just hard-coded to be “1”. So for brevity sake I’ll just use that for now.
import redstone.xmlrpc.XmlRpcClient;
public class XmlRpcPoster
{
public static void main( String[] args )
{
// Check command-line arguments
if( args.length < 3 )
{
System.out.println( "Usage: java XmlRpcPoster " );
}
else
{
// Get command-line arguments into variables
String sXmlRpcURL = args[0];
String sUsername = args[1];
String sPassword = args[2];
// Hard-coded blog_ID
int blog_ID = 1;
// XML-RPC method
String sXmlRpcMethod = "blogger.newPost";
// We'll hard-code our blog content for now as well
String sContent = "Hello XML-RPC World!";
// You can specify whether or not you want the blog published immediately
bool bPublish = true;
// Try block
try
{
// Create the XML-RPC client
XmlRpcClient client = new XmlRpcClient( sXmlRpcURL );
// Make our method call
Object token = client.invoke( sXmlRpcMethod, new Object[] { new Integer( blog_ID ), sUsername, sPassword, sContent, new Boolean( bPublish ) } );
// The return is a String containing the postID
System.out.println( "Posted : " + (String)token );
}
// Catch exceptions
catch( Exception e )
{
e.printStackTracke( System.err );
}
}
}
}
Wait a minute… there is no XML code anywhere in your example. Exactly! The Redstone API uses serializers to take the Object[]
you provide and create the appropriate XML. Spiffy, eh? Not only that, but if the return type of the method call happens to be a struct, then the token will contain a Map
that you can iterate over and retrieve values from.
I hope you enjoyed this little excursion into XML-RPC and Java. Stay tuned for my next post where I’ll be kicking this example up a notch to get more functionality and meta-data into our WordPress posting.