You are here

function xmlrpc_example_xmlrpc in xmlrpc 8

Implements hook_xmlrpc().

Provides Drupal with an array to map XML-RPC callbacks to existing functions. These functions may be defined in other modules. The example implementation defines specific functions for the example services.

This is the server part of the module, implementing a simple and little server with just two simple services. The server is divided in two different parts: the XML-RPC implementation (required) and a webform interface (optional) to configure some settings in the server side.

The XMLRPC server will define two different services:

  • subtract: perform the subtraction of two numbers. The minimum and maximum values returned by the server can be configured in the server configuration form.
  • add: perform the addition of two numbers. The minimum and maximum values returned by the server can be configured in the server configuration form.

If the result value for the operation is over the maximum limit, a custom error number 10001 is returned. This is an arbitrary number and could be any number.

If the result value for the operation is below the minimum limit, a custom error number 10002 is returned. Again, this value is arbitrary and could be any other number. Client applications must know the meaning of the error numbers returned by the server.

The following code is the XML-RPC implementation of the server part. The first step is to define the methods. This methods should be associated to callbacks that will be defined later.

Note: the XML-RPC server built in this module already includes several methods by default:

Service discovery methods:

  • system.listMethods: return a list of the methods the server has, by name.
  • system.methodSignature: return a description of the argument format a particular method expects.
  • system.methodHelp: returns a text description of a particular method.

Other:

  • system.multicall: perform several method calls in a single XML-RPC request.
  • system.getCapabilities: determine if a given capability is supported.

The methods defined by hook_xmlrpc() will be added to those provided by default by the XML-RPC server built in the module.

See also

hook_xmlrpc()

Related topics

File

xmlrpc_example/xmlrpc_example.module, line 75
Module file for xmlrpc_example module.

Code

function xmlrpc_example_xmlrpc() {
  $methods[] = [
    // First argument is the method name.
    'xmlrpc_example.add',
    // Callback to execute when this method is requested.
    '_xmlrpc_example_server_add',
    // An array defines the types of output and input values for this method.
    [
      // The first value is the return type, an integer in this case.
      'int',
      // First operand is an integer.
      'int',
      // Second operand is an integer.
      'int',
    ],
    // Include a little description that is shown when XML-RPC server is
    // requested for the implemented methods list.
    // Method description.
    t('Returns the sum of the two arguments.'),
  ];

  // The subtract method is similar to the addition, only the method name,
  // callback and description are different.
  $methods[] = [
    'xmlrpc_example.subtract',
    '_xmlrpc_example_server_subtract',
    [
      'int',
      'int',
      'int',
    ],
    t('Return difference of the two arguments.'),
  ];
  return $methods;
}