O'Reilly logo

Programming PHP, 3rd Edition by Rasmus Lerdorf, Peter MacIntyre, Kevin Tatroe

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

XML-RPC

XML-RPC and SOAP are two of the standard protocols used to create web services. XML-RPC is the older (and simpler) of the two, while SOAP is newer and more complex. Microsoft’s .NET initiative is based on SOAP, while many of the popular web journal packages, such as Frontier and Blogger, offer XML-RPC interfaces.

PHP provides access to both SOAP and XML-RPC through the xmlrpc extension, which is based on the xmlrpc-epi project (see http://xmlrpc-epi.sourceforge.net for more information). The xmlrpc extension is not compiled in by default, so you’ll need to add --with-xmlrpc to your configure line when you compile PHP.

Servers

Example 15-7 shows a very basic XML-RPC server that exposes only one function (which XML-RPC calls a “method”). That function, multiply(), multiplies two numbers and returns the result. It’s not a very exciting example, but it shows the basic structure of an XML-RPC server.

Example 15-7. Multiplier XML-RPC server
<?php
// expose this function via RPC as "multiply()"
function times ($method, $args)
{
  return $args[0] * $args[1];
}

$request = $HTTP_RAW_POST_DATA;

if (!$request) {
  $requestXml = $_POST['xml'];
}

$server = xmlrpc_server_create() or die("Couldn't create server");

xmlrpc_server_register_method($server, "multiply", "times");

$options = array(
  'output_type' => 'xml',
  'version' => 'auto'
);

echo xmlrpc_server_call_method($server, $request, null, $options);

xmlrpc_server_destroy($server);

The xmlrpc extension handles the dispatch for you. That is, it ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required