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.
Introduction
Interoperability 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 [1](WSO2 WSF/PHP) to develop a simple Web services client to communicate with a Microsoft .NET Web service.
Applies to
|
WSO2 WSF/PHP [2] |
1.2.0 |
|
Environment |
Windows or Linux |
Table of Content
- WSO2 Web Services Framework for PHP [2]
- Read the WSDL and Understand the Web Service Interface [2]
- Writing the WSF/PHP Client [2]
- Writing a WSDL Mode Client [2]
- Summary [2]
- Resources [2]
- Author [2]
WSO2 WSF/PHP [3] 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 [4] 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 [5].
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/ [6]. These service end points are hosted by Microsoft Corporation to test Web services interoperability.
http://131.107.72.15/SoapWsdl_BaseDataTypes_XmlFormatter_Service_Indigo/BaseDataTypesDocLitB.svc?wsdl [7]
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 [8], 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>
http://131.107.72.15/SoapWsdl_BaseDataTypes_XmlFormatter_Service_Indigo/BaseDataTypesDocLitB.svc?wsdl [9]
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!"
here [10].
Download WSO2 Web Services Framework for PHP [11]
Author
Dushshantha Chandradasa, Software Engineer, Quality Assurance, WSO2 Inc. dushshantha at wso2 dot com