iEntry 10th Anniversary RSS Newsletter Advertising
Visit Twellow.com
Text: Decrease Font Size Increase Font Size | Print Print Article | Share: Delicious Digg StumbleUpon Post to Twitter Post to Facebook
CommentWednesday, November 14, 2001

Communicating With XML-RPC

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.

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

News Tags: Server
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

Publish A Comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
2 + 4 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
SEARCH
Popular WPN Business Resources












Subscribe to WebProNews


Send me relevant info