Drupal XML-RPC Request

To use Drupal XML-RPC is easy, let’s use the classic example Getting the Name of a State to see how to make a XML-RPC request using Drupal.

In this classic example. we are sending a parameter along with the name of the remote method we are calling. UserLand Software runs a web service at betty.userland.com that has the 50 United States listed in alphabetical order. So if we ask for state 1, it returns Alabama; state 50 is Wyoming. The name of the method is examples.getStateName. Let’s ask it for state number 3 in the list:

$state_name = xmlrpc('http://betty.userland.com/RPC2', 'examples.getStateName', 3);

This sets $state_name to Arizona. Here’s the XML Drupal sends (we’ll ignore the HTTP headers for clarity from now on):

<?xml version="1.0"?>
<methodCall>
  <methodName>examples.getStateName</methodName>
    <params>
      <param>
        <value>
          <int>3</int>
        </value>
    </param>
  </params>
</methodCall>

Here’s the response we get from betty.userland.com:

<?xml version="1.0"?>
<methodResponse>
  <params>
    <param>
      <value>Arizona</value>
    </param>
  </params>
</methodResponse>

Notice that Drupal automatically saw that the parameter you sent was an integer and encoded it as such in your request. But what’s happening in the response? The value doesn’t have any type tags around it! Shouldn’t that be Arizona? Well, yes, that would work as well; but in XML-RPC a value without a type is assumed to be a string, so this is less verbose.

Now you see how simple it is to make an XML-RPC client call in Drupal. One line:

$result = xmlrpc($url, $method, $param_1, $param_2, $param_3...)
Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Technorati
  • Twitter
  • Yahoo! Bookmarks

About Shi Chuan

I am a web developer.
This entry was posted in CMS and tagged . Bookmark the permalink.

3 Responses to Drupal XML-RPC Request

  1. Pingback: » Drupal XMLRPC Web Services Database PHPJavascript Web Development Guide – My Personal Library of Tutorials and Scripts

  2. Pingback: » Drupal XML-RPC client PHPJavascript Web Development Guide – My Personal Library of Tutorials and Scripts

  3. Thank you for sharing such a useful information for drupal developers, howevere could you please clear more about how to setup the service on the website before using client?

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">