WSO2Con 2013 CFP Banner

WSO2 WSF/PHP Interoperability with Microsoft WCF

Discuss this article on Stack Overflow
By Marcus Chandradasa
  • 28 Jan, 2008
  • Level:  Introductory
  • Reads: 11352

In this article, Dushshantha Chandradasa describes how you can use the WSO2 Web Services Framework for PHP (WSO2 WSF/PHP) to develop a simple Web service client to communicate with a Web service in Microsoft .NET.

Marcus Chandradasa
Senior Engineer - Software Engineering
Highmark Health Services

Introduction

WSASInteroperability is the key feature that made Web Services the most widely used, successful off-spring of Service Oriented Architecture (SOA).

Most of the distributed system technologies such as Common Object Request Broker Architecture (CORBA) and Component Object Model (COM) are based on object technologies which are quite different from each other. As a result of this, requests from one platform to a service on another platform became very difficult. To resolve this issue, numerous products that provided integration middleware based on message orientation were created. On the other hand, Web services technology is built on the concept of messaging. Hence, requesters and services can live on different software and hardware platforms with channels connecting them. The implementation of the two ends are hidden from each other and they communicate through a common messaging format called SOAP. Web services use WSDL (Web Services Description Language) to describe the interface that a service offers to its users. Various specifications such as WS-Addressing ,WS-Security and WS-ReliableMessaging add various qualities such as security and reliability to SOAP massaging.

This article describes how you can use WSO2 Web Service Framework for PHP (WSO2 WSF/PHP) to develop a simple Web services client to communicate with a Microsoft .NET Web service.

 

Applies to

WSO2 WSF/PHP

1.2.0

Environment

Windows or Linux

 

Table of Content

  1. WSO2 Web Services Framework for PHP
  2. Read the WSDL and Understand the Web Service Interface
  3. Writing the WSF/PHP Client
  4. Writing a WSDL Mode Client
  5. Summary
  6. Resources
  7. Author

 

WSO2 Web Services Framework for PHP

WSO2 WSF/PHP is an excellent Web services frameworks that allows you to host and consume Web services in PHP. Its a PHP extension of WSO2 WSF/C based on Apache Axis2/C. WSF/PHP supports both SOAP 1.1, SOAP 1.2, WSDL 1.1, WSDL 2.0 as well as some of the key specifications in the WS-* stack including SOAP MTOM, WS-Addressing (version 1.0 and submission), WS-Security, WS-SecurityPolicy and WS-ReliableMessaging. Find more about WSO2 WSF/PHP and how you can install it on your system from here.

Here on, I assume that you have installed WSF/PHP in your system and is ready for use.

Let's see how we can write a simple WSF/PHP Web services client to utilize a simple .NET Web service. Following sections will guide you through the process. To demonstrate how, I'll use a .NET Web service hosted on Microsoft's interoperability lab http://mssoapinterop.org/ilab/. These service end points are hosted by Microsoft Corporation to test Web services interoperability.

 

Read the WSDL and Understand the Web Service Interface

In the following URL, you can find the description of a simple Web service hosted on Microsoft ilab. It's purpose is to test interoperability of basic data types. Let's use this service to see whether we can write a simple Web service's client to utilize one of its operations. Clicking the following URL, you can find the WSDL of the service.

http://131.107.72.15/SoapWsdl_BaseDataTypes_XmlFormatter_Service_Indigo/BaseDataTypesDocLitB.svc?wsdl

Now, you'll see a huge XML style file opening on your Web browser. This is the WSDL file that describes the interface of the Web service to its users. Other than this, everything - the implementation of the service, the language it is implemented in, on which platform it is hosted - is hidden from us. Let's take a look at the WSDL, and try to understand what it tells us about the service.

At the bottom of the file, you can see an XML element called service.

<wsdl:service name="BaseDataTypesDocLitBService">

<wsdl:port name="BasicHttpBinding_IBaseDataTypesDocLitB" binding="tns:BasicHttpBinding_IBaseDataTypesDocLitB">

<soap:address location="http://131.107.72.15/SoapWsdl_BaseDataTypes_XmlFormatter_Service_Indigo/BaseDataTypesDocLitB.svc"/>
</wsdl:port>

</wsdl:service>

It has the service name - <wsdl:service name="BaseDataTypesDocLitBService"> - and few other details about the service. Within the child port, you can find the endpoint address of the service. This is the address our client is going to send it's request to.

http://131.107.72.15/SoapWsdl_BaseDataTypes_XmlFormatter_Service_Indigo/BaseDataTypesDocLitB.svc

Now, find the binding of our service. Search for BasicHttpBinding_IBaseDataTypesDocLitB, which is the binding of the port in our service. You can find it in the following manner:

<wsdl:binding name="BasicHttpBinding_IBaseDataTypesDocLitB" type="tns:IBaseDataTypesDocLitB">
<soap:operation>
...
</soap:operation>
...
...
</wsdl:binding>

It has a number of operation elements that describe all of the operations the service can offer us.

We'll use RetString operation for our purpose.

<wsdl:operation name="RetString">
<soap:operation soapAction="http://tempuri.org/IBaseDataTypesDocLitB/RetString" style="document"/>
<wsdl:input name="RetString_RequestMessage_Body">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="RetString_ResponseMessage_Body">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>

The input message RetString_RequestMessage_Body describes how our request message should look like. The output message RetString_ResponseMessage_Body, describes, how the response message will be.

<wsdl:message name="RetString_RequestMessage_Body">
<wsdl:part name="inString" element="tns:inString"/>
</wsdl:message>
<wsdl:message name="RetString_ResponseMessage_Body">
<wsdl:part name="RetStringResult" element="tns:RetStringResult"/>
</wsdl:message>

Looking at the schema file http://131.107.72.15/SoapWsdl_BaseDataTypes_XmlFormatter_Service_Indigo/BaseDataTypesDocLitB.svc?xsd=xsd1, you can find what are tns:inString and tns:RetStringResult.

Now, we can think of the request payload that we are going to send to the service. It looks like :

<ns1:inString xmlns:ns1="http://tempuri.org/">Hello There!</ns1:inString>

 

Writing the WSF/PHP Client

Now, let's start writing our php client to send the above payload to the service and get the result.

Here is the complete script to do this.

<?php 
$reqPayloadString = <<<XML
<ns1:inString xmlns:ns1="http://tempuri.org/">Hello</ns1:inString>
XML;

try {
$client = new WSClient(
array("to"=>"http://131.107.72.15/SoapWsdl_BaseDataTypes_XmlFormatter_Service_Indigo/BaseDataTypesDocLitB.svc",
"action" => "http://tempuri.org/IBaseDataTypesDocLitB/RetString",
"useSOAP" => "1.1"));
$resMessage = $client->request($reqPayloadString);
printf("Response = %s <br>", htmlspecialchars($resMessage->str));
} catch (Exception $e) {
if ($e instanceof WSFault) {
printf("Soap Fault:%s\n", $e->code);
} else {
printf("Message = %s\n",$e->getMessage());
}

}
?>

Now, let's see what these lines really do.

First, create your XML payload to a variable called $reqPayloadString.

$reqPayloadString = <<<XML 
<ns1:inString xmlns:ns1="http://tempuri.org/">Hello</ns1:inString>
XML;

Then you have to create a new WSClient object addressing to our service endpoint, and with the SOAP action of the operation to send our request to the service. Also We have to say that we are using SOAP 1.1 version. You can find the SOAP action of the operation from the RetString operation element.

$client = new WSClient(
array("to"=>"http://131.107.72.15/SoapWsdl_BaseDataTypes_XmlFormatter_Service_Indigo/BaseDataTypesDocLitB.svc",
"action" => "http://tempuri.org/IBaseDataTypesDocLitB/RetString",
"useSOAP" => "1.1"));

Then you can send the request and get the response to a variable.

$resMessage = $client->request($reqPayloadString);

Of cource you can print the response if you need.

printf("Response = %s <br>", htmlspecialchars($resMessage->str));

Its always a good practice to use a try catch block to handle exceptions.

catch (Exception $e) {
if ($e instanceof WSFault) {
printf("Soap Fault:%s\n", $e->code);
} else {
printf("Message = %s\n",$e->getMessage());
}
}

Run your php client from the command line as

$> php myclient.php

or deploy it with your WSF/PHP enabled apache httpd server and run through the Web browser.

Now, you can see the result on command prompt or in a browser.

 

Writing a WSDL Mode Client

WSO2 WSF/PHP extension also supports contract first style of implementing Web services and clients. A user can simply provide a WSDL and implement Web services clients, based on the interface given in the WSDL. Let's see how we can use this feature to get our job done.

First, we have to create a WSClient object giving the WSDL of the service as a parameter. You can pass the following URL as the WSDL for the .NET Web service.

http://131.107.72.15/SoapWsdl_BaseDataTypes_XmlFormatter_Service_Indigo/BaseDataTypesDocLitB.svc?wsdl

Here's how you can do it:

$client = new WSClient(
array("wsdl" =>"http://131.107.72.15/SoapWsdl_BaseDataTypes_XmlFormatter_Service_Indigo/BaseDataTypesDocLitB.svc?wsdl"));

Now, you have to get a proxy object of the .NET service from the WSClient object you just created.

$proxy = $client->getProxy();

Using this $proxy object, we can send requests to the operations on the service. Lets send a request to RetString operation and see what we got.

$val = $proxy->RetString(array("Hello There!"))
echo $val."\n";

Here's the complete php script for the WSDL mode client for the .NET Web service:

<?php 
try {
$client = new WSClient(
array("wsdl" => "http://131.107.72.15/SoapWsdl_BaseDataTypes_XmlFormatter_Service_Indigo/BaseDataTypesDocLitB.svc?wsdl"));

$proxy = $client->getProxy();

$val = $proxy->RetString(array("Hello There!"));
echo $val."\n";
} catch (Exception $e) {
if ($e instanceof WSFault) {
printf("Soap Fault: %s\n", $e->Code);
printf("Soap Reason: %s\n", $e->Reason);
} else {
printf("Message = %s\n",$e->getMessage());
}
}
?>

Try running our client. You can see the service greeting you with a "Hello There!"

 

Summary

WSO2 WSF/PHP successfully interoperates with Microsoft WCF (.NET)Web services.

In this article, we have seen how the interoperability works with a simple .NET Web service in SOAP. WSF/PHP is also interoperable with .NET used with addressing, SOAP/MTOM(Attachments) and WS-Security. You may try writing your own client for the endpoints listed here.

 

Resources

 

Author

Dushshantha Chandradasa, Software Engineer, Quality Assurance, WSO2 Inc. dushshantha at wso2 dot com

WSO2Con 2014 USA