Tuesday, May 25, 2010

Web Service development using NuSOAP on XAMPP and client in .net using C#

Recently, one of my customers wanted to expose their Web Services for the service users. They wanted to keep the data centralized in their website and allow it to be accessed by Desktop Client software on user’s consoles. As the Website is built using Wordpress as a CMS, all the data is managed well by Wordpress. So we started exploring a PHP based solution to create the web service compliant to Web Services Description Language (WSDL) 1.1. We finally zeroed on NuSOAP.

Getting started with NuSOAP was not at all a difficult job (Thanks to Scott Nichol’s tutorial). Once you get a feel of it, just follow his Programming with NuSOAP Using WSDL tutorial and you are done!

For the Web Service development, I preferred running the NuSOAP on my http://localhost using XAMPP. I already had the wordpress with test data readily installed there in. As I was developing the Desktop Client App using .net, I felt it comfortable to have both NuSOAP and client to be on my machine, for development phase.

Getting NuSOAP working on XAMPP was as easy as in following steps:

  1. Download the latest version of NuSOAP (Version 0.9.5, while writing this post)
  2. Download the documents (Optional)
  3. Extract nusoap-0.9.5.zip and copy the lib directory into a folder in XAMPP environment. I created a folder webserv under my C:\xampp\htdocs\ folder and copied the lib directory there.
  4. Create a web service script. e.g. server.php in webserv folder. And in the server.php include the nusoap.php file. (Refer to the code snapshot below)

/*********** server.php ***************************************/

/* including MySql Database wrapper functions and config file */
require("Database.class.php");
require("config.inc.php");

/* including new soap functions */
require_once("lib/nusoap.php");

$ns="MyWebService";

$server = new soap_server();
$server->configureWSDL('B2BManager',$ns);
/* B2BManager is the name of web service */
$server->wsdl->schemaTargetNamespace=$ns;

Now you will be able to use NuSOAP calls in your web service, such as $server->register & $server->wsdl->addComplexType, as required.

After the Web Service setup, next thing was to request these services from the Desktop Application. This can be done by adding web service as a “Web Reference” in the .net solution. Following steps are involved in creating “Web Reference” in .net solution:

Add the Web Service URL, Discover and Add Reference. In the current example, the URL is : http://localhost/webserv/server.php?wsdl. Once this URL is entered, it will discover the services available. In this example, it discovered 1 service, i.e. B2BManager.

add-web-reference

 

add-web-reference-wsdl

Note: In case there’s any problem with the server.php, the generated WDSL may not be well formed. This results in failure in discovering the services. Try navigating to the URL on any browser to see, if the WSDL is proper. If the WSDL is proper, you should see a well formed XML stream as an output. (For e.g. try navigating to Amazon Web service; URL: http://webservices.amazon.com/AWSECommerceService/US/AWSECommerceService.wsdl.

Call the web service in your code. Declare the web service variable in the class that you want it to be called as below:

B2BManager.localhost.B2BManager webService_ = null;

webService_ = new B2BManager.localhost.B2BManager();

webService_.GetProducts();

2 comments:

  1. Hey, your post is cool but seems like i have stucked in somewhere.
    i have a problem where my microsoft visual studio 2008 cannot found the created web service (hello world), and i got no clue on what should i do, is there any coding in PHP need to be included so that i can use the web service? Thanks in advance!

    ReplyDelete
  2. I am quite sure they will learn lots of new stuff here,this information is very useful especially for students taking up computer courses,I’m very little directly into studying, however by some means I obtained to learn numerous content inside your webpage.A start up for a career in web hosting atlanta.

    ReplyDelete