0% found this document useful (0 votes)
209 views

XML-RPC Beginner Guide

XML-RPC allows sending HTTP requests containing XML data instead of GET/POST parameters. It uses HTTP as the transport and XML to encode calls and responses, allowing complex data to be sent between systems or applications. WordPress supports an XML-RPC API that allows remotely performing actions like getting comments, creating posts, and more. Requests are sent to xmlrpc.php and responses return XML data containing results.

Uploaded by

Andrea Sciamanna
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
209 views

XML-RPC Beginner Guide

XML-RPC allows sending HTTP requests containing XML data instead of GET/POST parameters. It uses HTTP as the transport and XML to encode calls and responses, allowing complex data to be sent between systems or applications. WordPress supports an XML-RPC API that allows remotely performing actions like getting comments, creating posts, and more. Requests are sent to xmlrpc.php and responses return XML data containing results.

Uploaded by

Andrea Sciamanna
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Some references

General definition of XMLRPC: http://en.wikipedia.org/wiki/XML-RPC


WordPress description of it: http://codex.wordpress.org/XML-RPC_Support
WordPress XMRLP API: http://codex.wordpress.org/XML-RPC_WordPress_API

How to test if it works in your site:


1. Go to http://your.domain.tld/xmlrpc.php
2. You should get "XML-RPC server accepts POST requests only."

How does it XML-RPC work?


Some definitions first.
HTTP - Is a protocol, but also a transport (of data) mechanism.
REQUEST - This is something we send to a server (the web server, for instance).

A typical request is when you open a website URL: in this moment the browser is actually sending an
HTTP request to the server, which contains, among other information, the URL you want.
RESPONSE - This is what the server respond to a request.

Again, when you ask the server for an URL, the response contains the HTML of that page (and other
hidden information).

GET and POST


You are probably familiar with HTML Forms.
As you may know, you can send the content of a form via POST or GET action.
When you use the GET method, after clicking the submit button, you will see all the form's arguments in the
URL.
This is exactly the way we use to pass arguments to WPML, for instance, with the ?lang=it. Whenever you
add arguments to an URL, you're actually sending data via the GET method. Everything after the question mark
(?) is called "querystring". All argument are separated from each other by an ampersand (&).
The other method is POST. When using the POST method, user won't see any information in the URL (unless
you are using both ways to send data, which goes out of this scope).
Both POST and GET are called request methods. There are other methods that can be used, but this also goes
out of this scope.
When using a request method, data is sent through what is called request message. A request message
contains several information, including the url, the method, and an optional body.
This body is where we put the data we want to send from the form.
Usually this data is sent in the same format of the GET querystring (ampersand separated names and values),
but there might be some exceptions, like in XML-RPC calls.
Once a request is sent, the server respond with a 'response message'. This message contains several
information, such a header (yes, another header!) and a body.
The header always contains a status. Usually if everything goes well, the status looks like HTTP/1.1 200 OK

(200 is the important part). If something goes wrong, the message change depending on the error (the well
known 404 Error for a not found page is the actual code provided in the header. 500 Error is a typical server
error message). The body is usually what we want to get from the server. Typically, is the HTML which is
rendered by the browser.

XML-RPC
XML-RPC is a remote procedure call (RPC) protocol which uses XML to encode its calls and HTTP as a
transport mechanism. [XML-RPC, Wikipedia]
Now let's forget about the form, and keep the everything else said so far.
Languages such as PHP and JavaScript can send HTTP requests without the need of an HTML form or any
HTML, as a matter of fact. And of course, they can read the response of that call.
If instead of sending a query string, we send an XML string, and the server accept this kind of requests, we can
use this system to send complex requests and, if needed return equally complex information.
I take for granted that you know what an XML file is.
Considering that an XML can basically contain any kind of information, you can do that:
XML-RPC method: wp.getComments Request:
<?xml version="1.0"?>
<methodCall>
<methodName>wp.getComments</methodName>
<params>
<param>
<value>
<string>1</string>
</value>
</param>
<param>
<value>
<string>xmlrpc-example</string>
</value>
</param>
<param>
<value>
<string>example-password</string>
</value>
</param>
<param>
<value>
<struct>
<member>
<name>post_id</name>
<value>
<int>41</int>
</value>
</member>
</struct>
</value>
</param>
</params>
</methodCall>

Response:

<?xml version="1.0"?>
<methodResponse>
<params>
<param>
<value>
<array>
<data>
<value>
<struct>
<member>
<name>date_created_gmt</name>
<value>
<dateTime.iso8601>20081121T02:54:08</dateTime.iso8601>
</value>
</member>
<member>
<name>user_id</name>
<value>
<string>1</string>
</value>
</member>
----8<----snip----8<----snip----8<----snip----8<----snip----8<----snip
<member>
<name>type</name>
<value>
<string/>
</value>
</member>
</struct>
</value>
<value>
<struct>
<member>
<name>date_created_gmt</name>
<value>
<dateTime.iso8601>20081120T23:49:42</dateTime.iso8601>
</value>
</member>
<member>
<name>user_id</name>
<value>
<string>1</string>
</value>
</member>
<member>
<name>comment_id</name>
<value>
<string>7</string>
</value>
</member>
----8<----snip----8<----snip----8<----snip----8<----snip----8<----snip
<member>
<name>author_ip</name>
<value>
<string>::1</string>
</value>

</member>
<member>
<name>type</name>
<value>
<string/>
</value>
</member>
</struct>
</value>
</data>
</array>
</value>
</param>
</params>
</methodResponse>

How to call this method?


This is a small example in PHP:
require "./IXR_Library.inc.php";
$rpc = new IXR_Client( "http://example.com/xmlrpc.php" );
$status = $rpc->query(
"wp.getComments", // method name
"1",
// blog id
"xmlrpc-example", // username
"example-password", // password
array(
"post_id" => 41
)
);
if( !$status ) {
print "Error ( " . $rpc->getErrorCode( ) . " ) : ";
print $rpc->getErrorMessage( ) . "\n";
exit;
}
$comment = $rpc->getResponse( );
print_r( $comment );

Of course, is possible to run actions which actually change the content in WordPress. For instance, you can
create a post, mark a comment as spam, etc.
WordPress provides APIs here: http://codex.wordpress.org/XML-RPC_WordPress_API
It's possible to modify WordPress XML-RPC methods, as well as creating brand new methods.
For instance, WPML does that, by extending the wp.newPost method with the language code. WPML also
adds new method and fields to translate a post, get translation informations about a post, etc.:
http://wpml.org/documentation/support/xml-rpc-language-interface/.

How to test XML-RPC?


You have several ways to do that.
You can create a script (as the above one) which sends requests to another site.
You can use some online service: http://www.tomhost.de/dev/tools/xmlrpc-tt/

You can use a browser extension: https://chrome.google.com/webstore/detail/xml-rpcclient/mbelaappiaalhoceejaopcghdlnhhain


You can use an extension of your IDE/Editor: PhpStorm has this tool natively, but if your editor/IDE don't
have this, there are good chances you can add it as a module, plugin, extension.

You might also like