Services 6.x-2.0 PHP example using XML-RPC for PHP

Last updated on
30 April 2025

This example is to show you a basic method call (so not including e.g. API keys) and how to construct the URL. On your XMLRPC server website you will need these Drupal modules enabled:

  • services
  • XMLRPC server for services
  • node services

You also need all relevant permissions set to allow access; and, for this example to work out of the box, uncheck the requirements of API keys and session IDs. For security reasons you will need to set all of this up eventually.

XML-RPC for PHP is a freely available standalone library for consuming XMLRPC feeds in PHP. It gets around common problems of e.g. other libraries being deprecated, or not available on your server. You can put it in e.g. sites/all/libraries rather than cluttering up your codebase with it.

Here's a basic example for how to use it to retrieve a node from a Drupal site. This client-side code is independent of Drupal: it's just PHP which would consume XMLRPC service provided by the Services modules on the existing Drupal site.

function rpcexample_retrieve_node($nid) {
  // Load the XML-RPC for PHP library from wherever you've put it
  // File path relative to the Drupal root
  require_once("sites/all/libraries/xmlrpc/lib/xmlrpc.inc");

  // Turn the node ID into a standard XMLRPC data type, for transmission
  $nid = new xmlrpcval($nid, "int");

  // Create an XMLRPC message 
  $m = new xmlrpcmsg('node.load',
    // Note that the "optional" second parameter is required, but it can be empty.
    // If you omit it, services returns wrong-parameter-number errors
    array($nid, new xmlrpcval(array(), "array"))
  );

  // Create a connection to the remote server
  // Note the root URL is services/xmlrpc. If you don't turn the
  // separate XMLRPC server module on, this will result in HTTP 404
  $c = new xmlrpc_client('/services/xmlrpc', 'remote-server.example.com', 80);

  // Send the message to the remote server and get the response
  $r = $c->send($m);

  // The return value is a collection of xmlrpcval objects
  // which you can loop over to turn back into PHP data types
  return $r->value();
}

The return value from XMLRPC for PHP is typically a parent-child tree of the slightly opaque xmlrpcval objects. That's a bit annoying, but here's a standard function which recursively converts these into nested arrays and structs instead:

/**
 * Turn xmlrpcval back into PHP-native, recursively
 */
function rpcexample_rpc_convert($x) {
  $keys = array_keys($x->me);
  $val = &$x->me[$keys[0]];

  switch($x->mytype) {
    // Scalar
    case 1:
      return $val;
    // Array
    case 2:
      $ret = array();
      foreach($val as $v) {
        // Don't preserve keys
        $ret[] = rpcexample_rpc_convert($v);
      }
      return $ret;
    // Struct
    case 3:
      $ret = array();
      foreach($val as $k => $v) {
        // Preserve keys
        $ret[$k] = rpcexample_rpc_convert($v);
      }
      return $ret;
  }
}

Help improve this page

Page status: Not set

You can: