RSS Archive Contact Us Advertise

IT Management Begins With Security
SecurityProNews > Articles > Application Development > Communicating With XML-RPC
Search:
[ articles_application_development ]

Communicating With XML-RPC



Bård Farstad
Contributing Writer
2001-11-14

SecurityProNews RSS Feed SecurityProNews RSS Feed


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

More articles_application_development Articles

SecurityProNews RSS Feed SecurityProNews RSS Feed


Get Your Site Submitted for Free in the World's Largest B2B Directory!

Email Address:
* URL:
*
*Indicates Mandatory Field

Terms & Conditions

iEntry Featured Services: Jayde Member Services | Forums | Freeware | Advertise with Us

Virus Warnings

Subscribe to
SecurityProNews FREE!



[ more newsletters ]

article resources
Search Articles:
[advanced search]

WebProWorld.com
Get in-touch with industry experts and leaders
Post your site for review by expert and peers
Ask Security, IT, Development and Design questions

Free Membership: Join Now!

Visit WebProWorld.com

Titan Quest Forum
The #1 Titan Quest forum
Halo 3 Forum
The best Halo, Halo 2, Halo 3 forum
Nintendo Wii
Nintendo Wii news and views
Mac Software
The best in OS X freeware
Graphics Forum
Your source for graphic tutorials
SecurityProNews.com | Breaking eBusiness News Get Your IT Questions Answered - Click Here SecurityProNews News Feeds