The most important thing to know about WordPress’ XML-RPC interface is how to send and receive data through it. The interface is turned off by default for security purposes, so when you’re ready to start using it, you’ll have to manually turn it on. Go to Settings » Writing and look for the XML-RPC option under “Remote Publishing.” Turn this on and you’re ready for the next step.
Next, you need to make a point of knowing the address of your XML-RPC server. This is really complicated. You might not be able to find it. But take your blog url and add “xmlrpc.php” to the end of it. See, that was incredibly difficult, wasn’t it? 🙂 WordPress makes it very easy to find and use the XML-RPC interface, so just make sure you’re always pointing your server at the right address. Most of the issues I’m emailed about when people are asking for help with their XML-RPC-powered plug-in are because their system is pointed at the blog URL rather than the xmlrpc.php file sitting at the blog URL. Make sure you point at the right file and everything else will work just fine.
The first step to making a request via XML-RPC is setting up your server object. You initialize the object with this code:
[cc lang=”php”]
$server = IXR_Client(“http://myblogaddress.com/xmlrpc.php”);
[/cc]
Now that your object is set up, let’s send a couple of commands to it. WordPress ships with two basic “demo” commands you can send through the API: One that says “hello” and one that adds two numbers. Let’s try saying “hello.”
The following command will send the “sayHello” demonstration request to the server. If things are set up properly, the server will return the string “Hello!”
[cc lang=”php”]
$client->query(“demo.sayHello”, “”);
[/cc]
You can see that the query method accepts two arguments, the name of the method you’re calling remotely and the arguments you wish to pass into it. If you want to pass multiple arguments into the remote method, you do so with an array:
[cc lang=”php”]
$client->query(“demo.addTwoNumbers”, array(5, 10));
[/cc]
This method call will remotely add the numbers 5 and 10. The server will pull the array apart, add the two arguments together, and return the sum. Simple, straightforward, easy to test.
Keep in mind that some of the more advanced API calls will require you to authenticate against your WordPress account. This is for security purposes so that not everyone can spontaneously post articles to your blog, moderate comments, or delete content. It keeps your site safe and secure. We’ll cover authentication while we cover the method calls that require it. But to give you a preview … all authentication requires is that you pass your username and password in addition to any other arguments. Not really too difficult, but if you forget it will cause a whole slew of problems.