Recently I was trying to figure out a way to automatically publish a summary of the preceding week’s C:SI statistics to this blog, similar to how some of the other Second Life® blogs publish traffic or signup or economic statistics, and being a WordPress user I thought it made sense to use XMLRPC and the metaWeblog API to accomplish this.
Using this API would allow me to programmatically post anything I wanted via a cron job, and seemed cleaner and less fragile than attempting to insert posts directly into the database.
Unfortunately, it was very difficult to find good code and information that did precisely what I wanted. The most frequently referred-to code for doing so just flat-out didn’t work and was not at all documented, but it was far and away the most frequently quoted sample on Google.
I finally managed to find decent enough documentation on Six Apart on what the various metaWeblog methods were expecting for input, and somewhere (I cannot remember now out of dozens of pages visited) I found the following snippet which is pretty lightweight and works perfectly for my needs.
I know it’s an off-topic post for what this blog is usually about, but I wanted to make sure that there was at least one more place on Google where someone with needs as simple as mine could find a snippet of PHP code to very easily post to a WordPress blog (or any other blog that supports metaWeblog API, there are several).
PHP Code:
//requires xmlrpc.inc from http://phpxmlrpc.sourceforge.net/ require_once(’xmlrpc.inc’); $g_blog_url = "http://path.to.blog/xmlrpc.php"; $g_id = "username"; $g_passwd = "password"; $GLOBALS[’xmlrpc_internalencoding’] = ‘UTF-8′; // http://www.sixapart.com/developers/xmlrpc/metaweblog_api/metaweblognewpost.html function metaWeblog_newPost( $blogid, $title, $content, $tags="", $category="" ) { global $g_id; global $g_passwd; global $g_blog_url; $client = new xmlrpc_client( "{$g_blog_url}"); $f = new xmlrpcmsg("metaWeblog.newPost", array( new xmlrpcval( "{$blogid}", "string"), // BlogID (Ignored) new xmlrpcval( $g_id, "string"), // User new xmlrpcval( $g_passwd, "string"), // Pass new xmlrpcval( // body array( ‘title’ => new xmlrpcval($title, "base64"), ‘description’ => new xmlrpcval($content, "base64"), ‘category’ => new xmlrpcval($category, "base64"), ‘mt_keywords’ => new xmlrpcval($tags, "base64"), ), "struct"), new xmlrpcval(true, "boolean") // publish ) ); $f->request_charset_encoding = ‘UTF-8′; $response = $client->send($f); return $response; }
Popularity: 49% [?]







Latest Comments