Submit Your Site For Free!

Email Address:
* URL:
*
*Indicates Mandatory Field

Terms & Conditions

Dev Newz
FlashNewz
DevWebPro










Communicating With XML-RPC

By Bård Farstad
Expert Author
Article Date: 2001-11-14

This article will show you an easy way to write a simple XML-RPC server and client using the eZ xmlrpc software package available from developer.ez.no.

What is XML-RPC?
XML-RPC is a Remote Procedure Calling protocol that works over the Internet using HTTP as a transport agent. The requests and responses are defined in XML. For the XML RPC spec go to XML-RPC spec.

This protocol enabled communication over different operating systems using different programming languages.

The Server
The code snippet below shows how you can implement a simple server using eZ xmlrpc. The server has one function, "myFunc", which returns a text string. This is the simplest example of the implementation of a server.

ob_start();

// include the server
include_once( "ezxmlrpc/classes/ezxmlrpcserver.php"
);

// include the datatype(s) we need
include_once( "ezxmlrpc/classes/ezxmlrpcstring.php"
);

$server = new eZXMLRPCServer( );

$server->registerFunction( "myFunc" );

// process the server requests
$server->processRequest();

function myFunc( )
{
return new eZXMLRPCString( "This command was run by xml
rpc" );
}

ob_end_flush();

As you can see you only have to create a function and register the function with the server with the $server->registerFunction() function. To implement the function you write a normal php function which returns a valid eZXMLRPC datatype; eZXMLRPCInt, eZXMLRPCString, eZXMLRPCBool, eZXMLRPCDouble, eZXMLRPCBase64, eZXMLRPCArray or eZXMLRPCStruct.

If you want to have parameters to the function you can add this with the second argument to registerFunction().

To register a function which takes an argument of the type int you write the code shown below. You can add as many parameters as you want.

// register the function
$server->registerFunction( "foo", array( new
eZXMLRPCInt() ) );

// implement the function
function foo( $args )
{
$tmp = new eZXMLRPCString( "You sendt me: " . $args[0]->value()
);
return $tmp;
}

The Client
The code snippet below shows how you can create a XML-RPC client. You create a client object which is responsible for the communication with the server. The call defines which function you want to request and the parameters, if any, you want to send. When you send the call you get a response, this response can be a fault or a normal response. If the response returned an error you can get the error message and error number.

include_once( "ezxmlrpc/classes/ezxmlrpcclient.php" );
include_once( "ezxmlrpc/classes/ezxmlrpccall.php" );

include_once( "ezxmlrpc/classes/ezxmlrpcstring.php" );

// create a new client
$client = new eZXMLRPCClient( "your.domain.com",
"/xmlrpc/minimalserver.php" );

// create a new call
$call = new eZXMLRPCCall( );
$call->setMethodName( "myFunc" );

// send the request
$response = $client->send( $call );

// check for fault
if ( $response->isFault() )
{
print( "The server returned an error (" . $response->faultCode()
. "): ".
$response->faultString() .
"<br>" );
}
else
{
// print out the results
$result = $response->result();

print( "The server returned: " . $result->value()
. "<br>" );
}

What Now?
Now you know how you can implement a server and/or client for distributed computing. There are many ways to use this software for communication between web sites. You can even write a command line client to your web server if you want.

About the Author:
Zez is a community website for developers, graphics and content designers. We aim to satisfy the intermediate to expert developers. For more information please visit http://www.zez.org




Newsletter Archive | Article Archive | Submit Article | Advertising Information | About Us | Contact

Dev Newz is an iEntry, Inc.® publication - 1998-2008 All Rights Reserved Privacy Policy and Legal