This post was created from java code. Before I show you this code I will give some background how this post and the code were created.
The following is a transcript of the little Q&A session that was playing in my head:
That depends on how wordpress interfaces with the outside world.
(after some googling…) The wordpress codex mentions some api’s that are supported through xml-rpc.
That’s the same question that is asked on the xml-rpc home page.
This is the answer they give:
It’s a spec and a set of implementations that allow software running on disparate operating systems, running in different environments to make procedure calls over the Internet.
It’s remote procedure calling using HTTP as the transport and XML as the encoding. XML-RPC is designed to be as simple as possible, while allowing complex data structures to be transmitted, processed and returned.
Furthermore, xml-rpc looks pretty simple looking at the wikipedia examples.
Having scanned through the specs, I can’t find a reason why not, but I really need an implementation to find that out.
The list of implementations at the xml-rpc home page shows a number of java implementations and one of them is from good old apache so I will give that a try.
Ok.
The php code that can be downloaded from dented reality shows the api’s in action. And the actual implementation of these api’s can be found in the xmlrpc.php file which is part of the wordpress distribution.
Ok. Let’s start with a little auxiliary class to keep some state:
package rintcius.blog;
public class BlogInfo {
private String apiKey;
private String userName;
private String password;
private String blogId;
public BlogInfo(String apiKey, String userName,
String password, String blogId) {
this.apiKey = apiKey;
this.userName = userName;
this.password = password;
this.blogId = blogId;
}
public String getApiKey() {
return apiKey;
}
public String getBlogId() {
return blogId;
}
public String getPassword() {
return password;
}
public String getUserName() {
return userName;
}
}
Here’s the class that does the posting:
package rintcius.blog;
import org.apache.xmlrpc.XmlRpcException;
import org.apache.xmlrpc.client.XmlRpcClient;
public class BlogPoster {
private static final String POST_METHOD_NAME = "blogger.newPost";
private XmlRpcClient client;
private BlogInfo blogInfo;
private PostType postType = PostType.draft;
public BlogPoster(XmlRpcClient client, BlogInfo blogInfo) {
this.client = client;
this.blogInfo = blogInfo;
}
public void setPostType(PostType postType) {
this.postType = postType;
}
public Integer post(String contents) throws XmlRpcException {
Object[] params = new Object[] {
blogInfo.getApiKey(),
blogInfo.getBlogId(),
blogInfo.getUserName(),
blogInfo.getPassword(),
contents,
postType.booleanValue()
};
return (Integer) client.execute(POST_METHOD_NAME, params);
}
public static enum PostType {
publish(true), draft(false);
private final boolean value;
PostType(boolean value) {
this.value = value;
}
public boolean booleanValue() {
return value;
}
}
}
And finally the example code that made this post happen:
package rintcius.blog;
import java.io.IOException;
import java.net.URL;
import org.apache.xmlrpc.client.XmlRpcClient;
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
import rintcius.blog.BlogPoster.PostType;
import rintcius.util.java.FileUtils;
public class PostExample {
public static void main(String[] args) throws Exception {
// the url of your xmlrpc.php, typically
// of the form http://your.domain.here/wordpress/xmlrpc.php
String xmlRpcUrl = args[0];
// this key is not used in my wordpress version
String apiKey = args[1];
String userName = args[2];
String password = args[3];
// in my wordpress version the blogId is "1"
String blogId = args[4];
XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
config.setServerURL(new URL(xmlRpcUrl));
XmlRpcClient client = new XmlRpcClient();
client.setConfig(config);
BlogInfo blogInfo = new BlogInfo(apiKey, userName, password, blogId);
BlogPoster poster = new BlogPoster(client, blogInfo);
poster.setPostType(PostType.publish);
poster.post(contents());
}
private static String contents() throws IOException {
// According to the wordpress post format the title and
// category id of the post get some special mark up.
return "<title>Look how this wordpress post got created from java!</title>"
+ "<category>6</category>"
+ FileUtils.getContentsOfResource("rintcius/blog/post.txt");
}
}
… or was it this post making the code happen?
Posted: July 16th, 2006 under Java.
Comments: none
©2006 - 2012 Rintcius Blok