XML-RPC in Drupal 7


XML - RPC protocol was originally created by web developer Dave Winner from "UserLand software company" in cooperation with Microsoft in 1998. However, Microsoft Corp. soon found this protocol too simplistic, and started to expand its functionality (that later resulted in establishing the extended protocol SOAP). Despite the rejection by Microsoft, XML - RPC standard, with its extraordinary simplicity, has so far charmed many programmers. (Wikipedia

In Drupal XML - RPC began it's existence in 4.6 version. Some minor changes in its operation occured after the release of Drupal 7.

First, let's set the tasks to ourselves and try to suggest the idea what we can utilize the XML - RPC protocol for. We've got 2 websites at our disposal:

  • website "A"
  • website "B"

The tasks are as follows:

1. learning all possible site "B's methods while staying onsite "A"

2. creating the functional for the content quantity output. Staying on site "A" we're supposed to controle the numbers of nodes on site "B" and vica verse.

3. creating a content functional of the site "B" while you stay on site "A" and vica verse


Drupal has a list of system methods, which are available immediately after the installation's done. The list of standard methods is as follows:

1. system.multicall- a systematic method, that allows for activating additional methods.

2. system.methodSignature- returns an array, the description of the data return type and the required data type for the selected method.

3. system.getCapabilities- returns a description of the XML - RPC specification supported by this server.

4. system.listMethods-  displays a list of available methods.

5. system.methodHelp- displays the description line for the specified method.

For solving the problem we need a method system, listMethods which displays all the methods of the site.

Reguest of any method is performed by using xmlrpc($url, $args, $options = array())

Let's look at the function arguments, there are only three of them:

1. $url -the absolute addresses for file request, like «/». 

2. $args -associative array whose keys reflected the methods which arguments are passing to the appropriate method. If several methods are not specified system.multicall are not satisfied too.

3. $options(not obvious argument) - the parameters of the query.

The only thing to do to get all site "B" methods is just to write the following line of code:

$methods = xmlrpc('', array('system.listMethods' => array())

as a result of the query, we will print an array with all available methods for site "B". Also, you can get all available methods of the current site.  You need only one argument of XML - RPC function to be changed that is to change website to which you address.

Task # 2

In order to get the number of nodes on another site, we should create our own method. Method can be create using hook_xmlrpc().

In hook we must return an array that has a name of method, the output type of argument (if any) and the description of the method (not obligatory). 

In the method we can send data types: 

  • boolean
  • double
  • int
  • array
  • struct
  • data;
  •  base 64
  • string

Example of hook_xmlrpc() realization: 

function internetdevels_xmlrpc_xmlrpc() {   
  $methods[] =  array(   
    'id_xmlrpc.node_count', // указываем название метода
    '_id_xmlrpc_node_count', // функция, которая запускаеться при вызове метода
    array('int'), // 1 значением массива обозначаем тип данных, 2 значение массива будет 1 аргументом в функции метода, 3 значение будет 2 аргументом фунукции и т.д.
    t('Return count of node') // описание к методу   
return $methods;   

So, we have anounced our hook_xmlrpc. The function will perform a simple select to the database and will return the number of records from the table node. Here is the function itself:

function _id_xmlrpc_node_count() {   
  return db_select('node', 'n')   

Pay attantion that the method and function should be the same as on the site "A" and the site "B".

Now we can get the number of entries in the table of  site "A" begin on site "B". In my case, the query would be:

$ncount  = xmlrpc('', array('id_xmlrpc.node_count' => array()));

in $ncount in case of successfull request, we will have a number, that will show the amount of records in the table node on site "B". 

Task #3

In this task we also need to declare a method. In my case I will ask hook_xmlrpc the following method:

$methods[] =  array(   
    array('string', 'array'),   
    t('Return count of node')   

we will return a reference to the created material, function will have one argument that will contain an array of :

function _id_xmlrpc_node_new_page($data) {   
  $node = new stdClass;   
  $node->title   = $data['title'];   
  $node->type    = 'page';   
  $node->created = REQUEST_TIME;   
  $node->status  = 1;   
  $node->body['und'][0]['value'] = $data['body'];   
  if (is_numeric($node->nid)) {   
    return url('node/' . $node->nid, array('absolute' => TRUE));   
  else {   
    return '';   

then we can call the method that will create content and if the content successfully create , in response to a request we will get a link.

Now beeing on the site "A" we can create a new node on the site "B". In my case, to create a content on another site I will describe function in this way:

$options = array('id_xmlrpc.node_new_page' => array( // создаем переменную с названием метода и опциями запроса
      'title' => 'Title of the new node', // заголовок контента
      'body'  => 'Body of the new node' // текст контента
// запускаем функцию xmlrpc   
$node_link  = xmlrpc('', $options); 

If the request successfull, in $node_link  we will have a link on the content, which we created.


Despite the fact that XML- RPC protocol has been long since designed, it is still relevant. It can help solve various problems of (2 or more) sites' simultaneous interraction. It is going to take just one function to build a query, with some simple parameters specified, to eventually make the work with the XML - RPC protocol easy and smooth.

5 votes, Rating: 5

Read also


The following article is giving a description of Titanium Appcelerator platform opportunities....


In theprevious article we shared about creating install profiles for Drupal 6. In this article it'll be described how to create...


In Drupal 7 API there is a chapter dedicated to site theming. All elements of site, without exclusion, should go through the theming process. But first of all theming itself should be set up.


In this post the bases of creating environment for Drupal projects development on the basis of Debian 6 "Squeeze" will be described. Having this, everyone will be able to practice setting OS...


Being involved in development process one has got to realize actions/commands for which he has not been granted enough rights or the user whose server has launched and executed Drupal, is lacking...

Need a quote? Let's discuss the project

Are you looking for someone to help you with your Drupal Web Development needs? Let’s get in touch and discuss the requirements of your project. We would love to hear from you.

Join the people who have already subscribed!

Want to be aware of important and interesting things happening? We will inform you about new blog posts on Drupal development, design, QA testing and more, as well news about Drupal events.

No charge. Unsubscribe anytime