Page 344 - Open Soource Technologies 304.indd
P. 344

Web Technologies-I



                   Notes                        Basic XML-RPC server

                                 <?
                                 php // this is the function exposed as “multiply( )” function times ($method, $args)
                                 {
                                 return $args[0] * $args[1];
                                 }

                                 $request = $HTTP_RAW_POST_DATA;
                                 if (!$request) $request_xml = $HTTP_POST_VARS[‘xml’];
                                 $server = xmlrpc_server_create( );
                                 if (!$server) die(“Could not 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 works out which method the
                                 client was trying to call, decodes the arguments and calls the corresponding PHP function, and
                                 returns an XML response that encodes any values returned by the function that can be decoded
                                 by an XML-RPC client.
                                 Create a server with xmlrpc_server_create( ):
                                 $server = xmlrpc_server_create( );

                                 Expose functions through the XML-RPC dispatch mechanism  using xmlrpc_server_register_
                                 method( ):

                                 xmlrpc_server_register_method(server, method, function);
                                 The method parameter is the name the XML-RPC client knows. The function parameter is the
                                 PHP function implementing that XML-RPC method. In the case of above Example, the multiply
                                 ( ) method is implemented by  the times( ) function. Often  a server will call xmlrpc_server_
                                 register_method( ) many times, to expose many functions.
                                 When you have registered all your methods, call xmlrpc_server_call_method( ) to do the
                                 dispatching:

                                 $response = xmlrpc_server_call_method(server, request, user_data [, options]);
                                 The request is the XML-RPC request, which is typically sent as HTTP POST data. We fetch that
                                 through the $HTTP_RAW_POST_DATA  variable. It contains  the name of the method  to be
                                 called, and parameters to that method. The parameters are decoded into PHP data types, and
                                 the function (times ( ), in this case is called). A function exposed as an XML-RPC method takes
                                 two or three parameters:
                                 $retval = exposed_function(method, args [, user_data]);
                                 The method parameter contains the name of the XML-RPC method (so you can have one PHP
                                 function exposed under many names). The arguments to the method are passed in the array
                                 args, and the optional user_data parameter is whatever the xmlrpc_server_call_method( )’s



        338                               LOVELY PROFESSIONAL UNIVERSITY
   339   340   341   342   343   344   345   346   347   348   349