The Incutio XML-RPC Library for PHP (IXR_Library)

Current Version: 1.7.1 (beta) 26May2005 [Permalink]

This is a modification of Simon Willison's IXR_Library - the Incutio XML-RPC Library for PHP.
Update: 20Feb2007. Sebastian Berm has made some modifications to the script to handle certificates without a CA. You can get Sebastian's updated version of the code and read all about it on his blog .
RELEASE POST AND COMMENTS

Release post (and comments)

DOWNLOAD

IXR_Library.inc.php.txt

ORIGINAL VERSION

The official version of this Library was 1.7 (beta) (Released 23May2005) and can be found at :

http://scripts.incutio.com/xmlrpc/

REQUIREMENTS

Tested on PHP 4.3 with curl 7.10.6.

I do not have access to a PHP5 system, and therefore would appreciate any bug reports or problems on this platform

USAGE

This modification adds three new features to the previous 1.7 (beta) release. They are :

  1. Client with SSL support (IXR_ClientSSL), including using client side certificates and CA files
  2. Server with object support (IXR_ClassServer), which can present methods from a variety of objects loaded in at run-time
  3. Ability for IXR_Server to wait before serve()-ing
IXR_ClientSSL - SSL Enabled XML-RPC Client

The IXR_ClientSSL class provides SSL support to the XML-RPC client using the cURL libraries.

As well as providing "normal" client SSL communications (throwaway keys), it also provides the ability for developers to specify the keys to be used in the transaction:

  • setCertificate allows the developer to specify the client certificate and (matching) private key (and passphrase) to use in the communications. This allows for the XML-RPC client to communicate with HTTPS servers which are configured to only allow clients presenting a valid certificate from an internal CA or other special situations.
  • setCACertificate allows the client to check that the XML-RPC server's SSL certificate is valid, by checking for the CA in it's signature chain. This allows the client to ensure that a server secured by an internal/private CA is, in fact, who it thinks it is.
IXR_ClientSSL example
//Connect to https://xmlrpc.example.com:443/server.php
$client=new IXR_ClientSSL('https://xmlrpc.example.com', '/server.php');

//Tell the client where the client certificates and keys are
$client->setCertificate('myPublicCert.pem', 'myPrivateKey.pem', 'my_secret_password');

//Tell the client where the CA certificate is (to validate the server's certificate)
$client->setCACertificate('theirCACert.pem');

//Send and process the output - same as IXR_Client
$client->query('Foo.barMe', 'Jason');
print $client->getResponse();
IXR_ClassServer - Object-Orientated Server

The IXR_ClassServer class provides a facility to build up the methods for an XML-RPC server based upon a variety of different objects created at runtime.

This provides the developer with the flexibility to use objects to represent their business logic, but expose only specific methods of these objects that are required. This means that your XML-RPC methods can work within an instantiated object, call member functions, access class fields and the such.

For example:

IXR_ClassServer example - Object to expose
class Foo
{
var $_bar;

function Foo()
	{
	$this->_bar="BAR";
	}

function barMe($name)
	{
	return $this->_bar + $name;
	}

function barThem($me, $friend, $buddy)
	{
	return $this->barMe($me) + " and " +
		$this->barMe($friend) + " and " +
		$this->barMe($buddy);
	}

//Personally, I like to wrap methods with multiple arguments in another function
//This is because the real function (logically) needs multiple arguments.
//XML-RPC wise, passing in a structure/hash/dictionary/whatever works out nicer (and clearer)
function _xmlrpc_barThem($args)
	{
	return $this->barThem($args['me], $args['friend'], $args['buddy']);
	}
}
		

Could be exposed as such:

IXR_ClassServer example - Preparing the XML-RPC server
//We want to present out methods in the form of OBJECTNAME.METHODNAME
//First argument is the delimiter (in this case ".")
//Second argument ($wait) tells the server not to process/serve immediately - we
//need to add our objects first!
$server=new IXR_ClassServer(".", true);

//Create our Foo object
$fooObject=new Foo();

//Register Foo object's methods
//First argument is the object
//Second argument is the array of methods to expose
//Third argument is the name of the Object we are exposing (OBJECTNAME above)

$server->registerObject($fooObject,
		array(
			'barMe',
			array('_xmlrpc_barThem', 'barThem')
		)
		, "Foo");

//Notice that for the barThem method, we specify the method as an array rather
//than just the method name (as we do with barMe). This is because we want to call
//the wrapper (_xmlrpc_barThem) but have it exposed (aliased) as barThem.

//Add more objects, do processing, whatever

//Finally, serve the request
$server->serve();
  
IXR_Server delayed processing

Finally, the addition of a new argument to the IXR_Server constructor called $wait allows for the processing of the server to be delayed.

By passing TRUE to this argument, the object will not serve() in the constructor, but will rather wait until later for a serve() call. This is not only required for the IXR_ClassServer class, but also allows you to create the server object, perform other processing and then finally process the XML-RPC request.

IXR_Server example - Delayed processing
//Prepare array of callbacks
$callbacks=array();

//Prepare the server
$server=new IXR_Server($callbacks, false, true);

//Do more processing, which would not normally be possible

//Finally, serve the pages
$server->serve();