CHAPTER 8 – XML-RPC – The Server

Writing the server is not much harder than writing the client. Instead of including the XML/RPC.php file, we now include the file that implements the server functionality: <?php require("XML/RPC/Server.php"); Next, we implement the functions themselves: function hello ($args) { /* The getValues() method returns an array with all * parameters passed to the function, converted from * XML RPC types to PHP types with the * XML_RPC_decode() function */ $vals = $args->getValues(); /* We simply return an XML_RPC_Values containing the

* result with the 'string' type */

return new XML_RPC_Response( new XML_RPC_Value("Hi {$vals[0]}!", 'string') ); } function add ($args) { $vals = $args->getValues(); return new XML_RPC_Response( new XML_RPC_Value($vals[0] + $vals[1], 'double') ); } To make the functions available to the outside, we need to define the methods by putting the function name, signature, and description string into an array containing an element for each function. The signature is formatted as how the system.methodSignature should return it--an array with an array containing the types: $methods = array( 'hello' => array ( 'function' => 'hello', 'signature' => array( array( $GLOBALS['XML_RPC_String'], $GLOBALS['XML_RPC_String'] ) ), 'docstring' => 'Greets you.' ), 'add' => array ( 'function' => 'add', 'signature' => array( array( $GLOBALS['XML_RPC_Double'], $GLOBALS['XML_RPC_Double'], $GLOBALS['XML_RPC_Double'] ) ), 'docstring' => 'Adds two numbers' ) ); We m a k e t h e d e fi n e d m e t h o d s av a i l a b l e b y i n s t a n t i a t i n g t h e XML_RPC_Server class. The constructor of this class handles parsing the request and calling the functions. You need to do nothing on your own, unless you want more advanced features that fall outside of the scope of this chapter. $server = new XML_RPC_Server($methods); ?> With this, we conclude XML-RPC.

Post Comment
Login to post comments