0% found this document useful (0 votes)
29 views

CSE2045Y Web Application Development

This document discusses web services and provides an example of creating a simple web service. It begins with an overview of web services and how they work using XML, SOAP, and WSDL. It then explains the roles of WSDL and SOAP in more detail. The document demonstrates how to create a SOAP web service using PHP and the NuSOAP library that returns lecturer experience data based on a module name. It shows the process of configuring the SOAP server, defining the WSDL, registering a getExperience function, and constructing the SOAP response.

Uploaded by

splokbov
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

CSE2045Y Web Application Development

This document discusses web services and provides an example of creating a simple web service. It begins with an overview of web services and how they work using XML, SOAP, and WSDL. It then explains the roles of WSDL and SOAP in more detail. The document demonstrates how to create a SOAP web service using PHP and the NuSOAP library that returns lecturer experience data based on a module name. It shows the process of configuring the SOAP server, defining the WSDL, registering a getExperience function, and constructing the SOAP response.

Uploaded by

splokbov
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

CSE2045Y

Web Application Development

Lecture 10
Web Services

1
Agenda
• Web Services Overview
• Web Services Description Language (WSDL)
• Simple Object Access Protocol (SOAP)
• Web Service Example
– WSDL Structure
– SOAP Request
– SOAP Response
– Invoke operation
What are Web Services?
• A web service is any piece of software that makes
itself available over the internet and uses a
standardized XML messaging system. XML is used
to encode all communications to a web service.

• Web services are self-contained, modular,


distributed, dynamic applications that can be
described, published, located, or invoked over the
network.
How Does a Web Service Work?
• A web service enables communication among
various applications by using open standards such
as HTML, XML, WSDL, and SOAP. A web service
takes the help of:
– XML to tag the data.
– SOAP to transfer a message.
– WSDL to describe the availability of service.

• The basic web services platform is XML + HTTP.


Web Service Overview (1)

(SERVER) (CLIENT)
http://blog.krawler.com/2009/08/role-of-web-services-in-soa/
5
Web Service Overview (2)
• Publish – Service Producers register their service in the
SOA registry. Web Services Description Language
(WSDL) is used to describe a service.

• Discovery – Service Consumers make a request for a


service in the SOA registry. Universal Description,
Discovery and Integration (UDDI) standard is used for
locating a service in the registry.

• Communication – Simple Object Access Protocol


(SOAP) is used for facilitating communication between
Service Providers and Consumers.

6
WSDL
• WSDL is used to locate a web service and
invoke any of its publicly available functions.

• WSDL defines how:


1. The Web service is accessed (location),
2. The operations it performs (functions which can
be invoked)
3. How messages are passed (SOAP Request, SOAP
response)
4. The format of the messages (XML)
7
WSDL Structure
<definitions xmlns="http://schemas.xmlsoap.org/wsdl/">
<types>
<!-- definition of types used in WSDL -->
</types>
<message>
<!-- abstract definition of the data being transmitted
-->
</message>
<portType>
<!-- a set of abstract operations referring to input
and output messages -->
</portType>
<binding>
<!-- concrete protocol and data format specs -->
</binding>
<service>
<!-- specifies locations and bindings for a service -->
</service>
</definitions>
8
SOAP
• SOAP stands for Simple Object Access Protocol.
• SOAP is an application communication protocol.
• SOAP is a format for sending and receiving
messages.
• SOAP is platform independent.
• SOAP is based on XML.
• SOAP is a W3C recommendation.
Why SOAP?
• SOAP provides a way to communicate between
applications running on different operating systems,
with different technologies and programming
languages.

• A SOAP message is an ordinary XML document


containing the following elements:
– An Envelope element that identifies the XML document as
a SOAP message.
– A Header element that contains header information.
– A Body element that contains call and response
information.
– A Fault element containing errors and status information.
Soap Message Format

http://www.cs.toronto.edu/~wl/csc309/handouts/webservice.pdf
11
NuSOAP
• NuSOAP is a group of PHP classes that allow developers
to create and consume SOAP web services.

• It does not require any special PHP extensions.

• NuSOAP can be downloaded from:


– http://nusoap.sourceforge.net

• 2018WS example will use the NuSOAP library.


– Note: The WSDL file will be automatically generated when
using NuSOAP.
12
Structure of SOAP Request
(Simplified version)

<!-- Input message -->


<?xml version="1.0“ encoding="UTF-8"?>
<SOAP-ENV:Envelope>
<SOAP-ENV:Body>
<input>web</input>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

13
Structure of SOAP Response
(Simplified version)
<!– Output message -->
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope>
<SOAP-ENV:Body>
<output>
<LecturerExperience>
<lecturer>Lecturer 3</lecturer>
<module >CSE2024y-Object Oriented Programming</module>
<experience>10</experience>
</LecturerExperience>

<LecturerExperience>
<lecturer>Lecturer 2</lecturer>
<module > CSE2045y-Web Application Development</module>
<experience>3</experience>
</LecturerExperience>
</output>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope> 14
NuSOAP configuration
2018WS files Contents of NuSoap lib

• NuSoap lib is included


in 2018WS example.

15
module.html

16
module.html
module.html
server.php

19
server.php
• The purpose of the server is:
1. To create a SoapServer which will provide services
based on a specified WSDL.

Note that the WSDL file does not exist; we will be


creating the WSDL in server.php.

2. Register function(s) having similar name as


operations in the WSDL .

We will be defining a function getExperience(…)


that takes the module name as parameter.
20
Create a SoapServer which will provide
services based on a specified WSDL
• The steps to create the WSDL (using NuSOAP) are:
1. Include the NuSOAP library.

require_once('lib/nusoap.php');

2. Create the server instance and initialise WSDL support.


// Create the server instance
$server = new soap_server();

// Initialise WSDL support


$NAMESPACE = 'http://www.uom.ac.mu/experience';
$server->debug_flag=false;
$server->configureWSDL('Module', $NAMESPACE);
$server->wsdl->schemaTargetNamespace = $NAMESPACE;
21
Create a SoapServer which will provide services
based on a specified WSDL
3. Declare WSDL complex types
// ---- LecturerExperience ----

$server->wsdl->addComplexType(
'LecturerExperience',
'complexType',
'struct',
'sequence',
'',
array(
'lecturer' => array('name'=>'lecturer','type'=>'xsd:string'),
'module' => array('name'=>'module','type'=>'xsd:string'),
'experience' => array('name'=>'experience','type'=>'xsd:int')
)
);
22
Create a SoapServer which will provide services
based on a specified WSDL
3. Declare WSDL complex types (Continued)
// ---- LecturerExperienceArray[] -----

$server->wsdl->addComplexType(
'LecturerExperienceArray',
'complexType', Note that the SOAP
'array', Response will return an
'', array of LecturerExperience
'SOAP-ENC:Array',
array(),
array(
array('ref'=>'SOAP-ENC:arrayType',
'wsdl:arrayType'=>'tns:LecturerExperience[]')
),
'tns:LecturerExperience'
); 23
Register function(s) having similar name as
operations in the WSDL
• WSDL function(s) registration

// ==== WSDL FUNCTIONS REGISTRATION =====

$server->register('getExperience', // method name


array('input' => 'xsd:string'), // input parameters
array('output' => 'tns:LecturerExperienceArray'), // output parameters
$NAMESPACE); //namespace

Along with function declaration (getExperience), we


are also defining input and output SOAP parameters
24
getExperience(…) function
function getExperience($value) { Extract input parameter
$searchCriteria = $value[input'];
from SOAP Request
$sql = "SELECT CONCAT( L.Name, ' ', L.Surname ) AS Lecturer, CONCAT( M.ModuleCode, '-', M.ModuleName )
AS Module, L.Experience AS Experience FROM lecturers L, modules M
WHERE L.ModuleCode = M.ModuleCode
AND M.ModuleName LIKE '%".$searchCriteria."%' ORDER BY L.Experience
DESC;";

$result = mysql_query($sql);

$experience = array();
$experienceArray = array();

if(mysql_num_rows($result) > 0){


while ($row = mysql_fetch_assoc($result)) { SOAP Response
$experience = array('lecturer'=>$row['Lecturer'],
‘module' => $row['Module'],
construction
'experience' => $row['Experience']); having same
$experienceArray[] = $experience;
} structure as
} else{
mysql_free_result($result); output message
$module = array('lecturer'=>'NO DATA', 'module' => 'NO DATA', 'experience' => 'NO DATA');
}
return $experienceArray;
mysql_close($con);
}
25
View the contents of the WSDL file (1)
• Once server.php is created, access it using localhost

• Click on WSDL to view


its contents

26
View the contents of the WSDL file (2)
• Click on
getExperience to
view the function
that would be
exposed as a web
service

• Note the Input


and Output types

27
client.php

28
client.php
• The purpose of the client is:
1. To create a SoapClient which will consume a
service by using a specified WSDL

The WSDL file has already been created in


server.php

2. Invoke the required operation (function) with


relevant parameter(s) as advertised by the WSDL

29
Create a SoapClient which will consume a
service by using a specified WSDL

require_once('lib/nusoap.php');

//Get input parameter


if(isset($_GET["txt_module"]))
$module = $_GET["txt_module"];

//Create a soap client


$url = “http://localhost/2018WS/server.php?wsdl ";
$client = new nusoap_client($url);

$err = $client->getError();
if ($err) {
echo '<p><b>Error: ' . $err . '</b></p>';
}
30
Invoke the required operation (function) with
relevant parameters as advertised by the WSDL
• Invoke the getExperience function
$args = array('input' => $module);
$response = $client->call('getExperience', array($args));

• Display SOAP Request/Response (XML format)

//Display SOAP Request/Response


echo '<h2>Request</h2>';
echo '<pre>' . htmlspecialchars($client->request, ENT_QUOTES) . '</pre>';
echo '<h2>Response</h2>';
echo '<pre>' . htmlspecialchars($client->response, ENT_QUOTES) . '</pre>';

31
SOAP Request/Response
(XML format)

32
Processing and displaying the SOAP
Response using XSLT
//Reading returned Object and creating XML
$XMLDocument = new SimpleXMLElement('<?xml version="1.0"
?><Experiences></Experiences>');

foreach($response as $record){
$experience = $XMLDocument->addChild('LecturerExperience');
$experience->addChild('Lecturer',$record[lecturer]);
$experience->addChild('Module',$record[module]);
$experience->addChild('Experience',$record[experience]);
}

// Apply XSLT to display the SOAP Response


$XSLDocument = new DOMDocument();
$XSLDocument->load("module.xsl");
$XSLProcessor = new XSLTProcessor();//PHP5
$XSLProcessor->importStylesheet($XSLDocument);
echo $XSLProcessor->transformToXML($XMLDocument);
33
SOAP Response (XSLT)
• The SOAP XML Response is processed using
the XSLTProcessor()

34
Running the 2018WS example (1)
• Download 2018WS from LCMS.

• Extract the files in a folder 2018WS in the root


directory (htdocs if you are using XAMPP)
– http://localhost/2018WS

• The database script experience.sql is also


included in the zip file
– Note: the database name is “experience”

35
Running the 2018WS example (2)
(Database tables)
• lecturers

• modules

36
Activity 1
• Determine how Web Service (2018WS example) will
change if both the module name and the minimum
lecturer experience are passed as input parameters.

• Highlight the codes that will change !


References
• CSE2041-Web Technologies II Lecture notes
• https://www.w3schools.com/xml/xml_soap.asp
• https://www.w3schools.com/xml/xml_wsdl.asp
• https://www.tutorialspoint.com/webservices/w
hat_are_web_services.htm

You might also like