Discussion Community

IT Acumens Forum => Programming Discussions for Engineers => Web Services in PHP => Topic started by: sukishan on Aug 23, 2009, 12:02 AM

Title: Creating an XML-RPC Web service - XML-RPC
Post by: sukishan on Aug 23, 2009, 12:02 AM
XML-RPC
Remote Procedure Calls are used to establish and facilitate transactions between two remote systems. Example of popular RPC implementations include DCOM and CORBA. XML-RPC is an established implementation of RPC that allows you to transport XML encoded data between two servers using HTTP. In the following examples, we will use an implementation of XML-RPC originally developed by Edd Dumbill ( http://xmlrpc.usefulinc.com/php.html (http://xmlrpc.usefulinc.com/php.html)).

To enable PHP XML-RPC functionality, you must download the XML-RPC toolkit available at the following link: http://sourceforge.net/project/showfiles.php?group_id=34455 (http://sourceforge.net/project/showfiles.php?group_id=34455)

Creating an XML-RPC Web service
The main include files we will be using are xmlrpc.inc (the base class library) and xmlrpcs.inc (the server class library). Here is how you implement a simple XML-RPC server using PHP. First, we bring in both the client and server libraries using include statements:

<?
include("xmlrpc.inc");
include("xmlrpcs.inc");
Then we define a new function called onttax. This function will be the backbone of a web service that will calculate the 15% Federal & Provincial sales tax for Ontario Canada based on a dollar amount. A parameter, which corresponds to the dollar amount, is passed into the function. The parameter is then converted to a scalar variable. Once the calculation is completed, a response is created (using the xmlrpcresp class) returning the value of the sales tax:


function onttax($par){
$amount=$par->getParam(0);
$amountval=$amount->scalarval();
$taxcalc=$amountval*.15;
return new xmlrpcresp(new xmlrpcval($taxcalc, "string"));
}
Then we can instantiate the server and serialize our onttax function back to the caller.


$server=new xmlrpc_server(array("taxcalc.onttax"=>array("function"=>"onttax")));
?>