intelliproject logo

Location: Desktop development - C#    License: The Intelliproject Open License (IPOL)

XML-RPC – communicating between a C# client and a PHP server

Posted by Silviu Caragea

This article describe how can you implement a simple XML-RPC application to communicate between a C# client and a PHP server

Skill: Intermediate

Posted: 19/05/2009

Views: 5324

Rating: 5.00 /5

Popularity: 1.51

Sign Up to vote for this article

Introduction

Remote procedure call (RPC) is an inter-process communication technology that allows a computer program to cause a subroutine or procedure to execute in another address space (commonly on another computer on a shared network) without the programmer explicitly coding the details for this remote interaction.

XML-RPC is a Remote Procedure Calling protocol that works over the Internet.

An XML-RPC message is an HTTP-POST request. The body of the request is in XML. A procedure executes on the server and the value it returns is also formatted in XML. Procedure parameters can be scalars, numbers, strings, dates, etc.; and can also be complex record and list structures.

Building a XML-RPC server in PHP

The simplest way to build a XML-RPC server in PHP is to use the "Incutio XML-RPC library (IXR)" (this solution is also used in Wordpress and other well known PHP projects).

As the author describe on his own website , The Incutio XML-RPC library (IXR) is designed primarily for ease of use. It incorporates both client and server classes, and is designed to hide as much of the workings of XML-RPC from the user as possible. A key feature of the library is automatic type conversion from PHP types to XML-RPC types and vice versa. This should enable developers to write web services with very little knowledge of the underlying XML-RPC standard.

In my example I defined 3 PHP functions :

  • function getServerTime($args)- this function return the current date time of the server
  • function getCountryList($args) - return a JSON encoded string which contain all countries which start with a string transmitted as parameter to this method. The countries are selected form a MySql database.
  • function getCountryDetails($args) - return a JSON encoded string which contain all available details about a country specified as first parameter. 

After we have defined our methods we must create the XML-RPC server.

As you can see , using "The Incutio XML-RPC library (IXR) " this is a very easy job .

You must create an array where the key is the method identifier (package.function_identifier) and the value is the function name . After you have created this array just passed it to the IXR_Server.

To install my little XML-RPC server you must create the database, running the rpc.sql script , and after that you must edit the "config.php" file with your MySQL server address , username and password .

Building a XML-RPC client in C#.NET

To build the XML-RPC client in C# I choose to use two open source libraries : XML-RPC.NET developed by Charles Cook and Json.NET developed by James Newton-King .

XML-RPC.NET is a library for implementing XML-RPC Services and clients in the .NET environment, supporting versions 1.0, 1.1, and 2.0 of the .NET runtime. The library has been in development since March 2001 and is used in many open-source and business applications.

The Json.NET library makes working with JavaScript and JSON formatted data in .NET simple. Quickly read and write JSON using the JsonReader and JsonWriter or serialize your .NET objects with a single method call using the JsonSerializer.

Both libraries are very well documented on their dedicated web pages.

To create our XML-RPC client we must create an interface derived from IXmlRpcProxy.In this interface we will add our definitions for all available methods (desired methods) which exist on our server side .

In our Form class we will declare a IServerInterface variable and we will instantiate this like in the following line :

private IServerInterface rpcProxy = XmlRpcProxyGen.Create();

To display the server time we will call  GetServerTime method :

First of all we set the xml-rpc proxy object the URL to our server (i.e. http://localhost/MyXmlRpcServer/web/xml-rpc.php). After that we call the GetServerTime and display the returned string in a message box.

To take all available countries which start with a specified text we will use the following code:

What's new here ? We are using Json.NET to decoding the result and to extract the fields we need . As you can see this is very easy to do using this library , and I think the code doesn't need any additional explanation.

How does it works ?

For example let's suppose that our client try to call GetServerTime to get the actual server date time .

The client will send to our server the following request via POST:

Request headers :

POST /MyXmlRpcServer/web/xml-rpc.php HTTP/1.0
Host: localhost
Content-Type: text/xml
User-Agent: The Client User Agent
Content-length: 112

Request body :

The server will receive this request and after it process the body will decide which method it must to called and which parameters (if any ) it must to pass to that function.

If the method it is not published on the server the response will be an error message, otherwise if the method exist the response will be in the following format :

After the client will receive this response It will parse the XML and will extract the useful information .

License

This article, along with any associated source code and files, is licensed under The Intelliproject Open License (IPOL)

About the author

Silviu Caragea

Silviu Caragea is the Founder, Administrator and Chief Editor who wrote and runs The IntelliProject.

He's been programming since 2000 and now he's student at The Faculty of Economic Cybernetics, Statistics and Informatics from Bucharest. In the same time he's working as software developer at Cratima Software, a Romanian software and web design company that activates both on the local and foreign market, providing its customers with software development services, internet and intranet solutions, web design, graphic design and IT consultancy.

His programming experience includes:
- C,C++, Visual C++(Win32 API, MFC, ADO, STL, DAO, ODBC, ATL, COM, DirectShow, DirectDraw, WTL)
- Open Source libraries :CURL & Boost
- HTML, CSS
- Java (SE,ME)
- JavaScript, Ajax, Google Web Toolkit (GWT)
- Php, MySQL
-Oracle, PL SQL
- C# .NET
-Objective C, IPhone SDK, Cocoa

Location: Romania
Ocupation: Software Engineer
Home page: http://www.intelliproject.net

Posted by leoganda at 13/06/2009 09:25
Hi hello, I've been looking for this tutorial and it's great! Thanks for the tutorial.
But I've a question about it,
I'm going to made a function add(int a,int b) on server side (php), and call the method in C#.

public interface IServerInterface : IXmlRpcProxy
{
[XmlRpcMethod("xmlrpc.add")]
string add(int a,int b);
}

But it returns error, "Exception : Response from server does not contain valid XML"

I had tried with single parameter, and it works, but stuck with more than 1 param.

Thanks.
Posted by Silviu Caragea at 13/06/2009 11:11
probably your function prototype on php server is not as follow :
function add($args)
{
$args = explode(" ", $args);
$a = $args[0];
$b = $args[1];
return $a+$b;
}
and in c# use the following declaration :
[XmlRpcMethod("xmlrpc.add")]
string add(int a,int b);


Posted by leoganda at 13/06/2009 20:22
@silviu caragea, still doesn't work.
Posted by leoganda at 13/06/2009 20:40
I got the solution, we don't need to explode the $args, so the code to be like this.

function add($args)
{
$a = $args[0];
$b = $args[1];
return $a+$b;
}

Sign up to post message on the article message board!