Several XML-RPC implementations are out there, but the one most widely used and also in active development is PEAR::XML-RPC, available at http://pear.php.net/package/XML_RPC/. It is a PEAR port of another XML-RPC library and adds some fixes that didn't make it in the original distribution.You can install it with pear upgrade XML_RPC, to get the latest and greatest version. To provide such a Web Service, you have to instantiate the XML_RPC_Server class and provide some information about the service: which function to use, its description, and, of course, the signature (which parameter types are passed, which data type is returned). The preceding code contains this information for a simple "add two numbers" Web Service. Information about the XML-RPC Web Service (xmlrpc-pear-server.php; excerpt)<?php require_once 'XML/RPC/Server.php'; $description = 'adds two numbers'; $signature = array( array('int', 'int', 'int') ); $xmlrpc = new XML_RPC_Server( array('phrasebook.php.add' => array( 'function' => 'add', 'docstring' => $description, 'signature' => $signature ) ) ); ?> Note that the signature contains the data types of all input parameters plus the data type of the return value; that's why 'int' appears three times and not just twice. Now the only thing missing is the actual business logic, the function to add. This is a bit tricky: First, the getParam() method returns all parameters provided to the function; then, the scalarval() retrieves the actual value of such a parameter. Then, the actual adding of the numbers can be done, by returning a suitable XML_RPC_Response object. For instance: function add($params) { $a = $params->getParam(0); $b = $params->getParam(1); if (isset($a) && isset($b)) { if ($a->scalartyp() == 'int' && $b->scalartyp() == 'int') { $a = $a->scalarval(); $b = $b->scalarval(); $c = new XML_RPC_Value($a + $b, 'int'); return new XML_RPC_Response($c); } else { global $XML_RPC_erruser; return new XML_RPC_Response(0, $XML_RPC_erruser, 'wrong parameters'); } } } |