If you notice, there are many desktop’s software that has an ability to remotely publish an article to blog without requirements to even login into the admin area. That software may firstly ask you to input your username and password, after that, you just need to write and publish inside the software without even visits the website itself.

How is it possible?

Well, thanks to XML-RPC protocol. Software from Desktop can directly interact with a blog. Most popular CMS today supports XML-RPC protocol such as WordPress, Joomla, and Drupal.

How Does this XMLRPC work?

Just put the program to send XML to contain any instruction and contents to Blog. Then the script of the website will select to approve to an instruction or not.

How to Programmatically sends a Post to WordPress via XML-RPC

XML-RPC uses HTTP as a transport mechanism and XML to encode its calls.  Therefore, we can use any programming language and can be utilized by any platform.

For now, I am going to show you how to write an XML-RPC using a PHP and WordPress as a platform to implements XML-RPC. To properly construct XML with PHP, we can use SimpleXML.

But for today lesson, we are going to use popular open source script called Incutio XML-RPC Library. It has everything we need to build XML-RPC encode.

And for flexibility to what we can send and to sanitize data like what is supposed to do.  We are better to use Inheritance as future proof just in case that the author of this script decides to change the code later.

Example XML-RPC Using PHP and Incutio

Includes an Incutio class

require_once("IXR_Library.php");

Now let’s create child class from Incutio


class XMLRPClientWordPress extends IXR_Client {
private $user = "";
private $pass = "";


// constructor
public function __construct($xmlrpcurl, $username, $password) {
parent::__construct($xmlrpcurl);

$this->user = $username;
$this->pass = $password;
}

// Method to transport our post to WordPress
function create_post($content,$status,$encoding='UTF-8') {

$params = array(0,$this->user,$this->pass,$content,$status);

$result =$this->query('metaWeblog.newPost', $params) ;
if(!$result) {
return false;
}
else
return $this->getResponse();

}

}


The explanation of the code above.

  • $xmlrpcurl = For WordPress usually the url like this http://yourdomain.com/xmlrpc.php
  • ‘metaWeblog.newPost’ = it indicates that we are going to create new post to worpress.
  • $content = Must be consisted with array parameters like as you can see below
 $content = array(
'title'= "Your title Post",
'description'= "Your Articles",
'post_type'= 'post'
)

There are more parameters such as categories, tags, allow comment or slug. But we will completely cover them in next article.

As you can see XMLRPC is fully equipped with many options that you can imagine to remotely control your WordPress’s site.Even WordPress itself using this to manage their trackback’s feature.

I Have developed many projects using PHP or C#, the only problem you may get is likely an encoding error. Always make sure to encode your XML with utf-8.