CSE2045Y Web Application Development
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.
(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.
6
WSDL
• WSDL is used to locate a web service and
invoke any of its publicly available functions.
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.
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
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.
require_once('lib/nusoap.php');
$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
$result = mysql_query($sql);
$experience = array();
$experienceArray = array();
26
View the contents of the WSDL file (2)
• Click on
getExperience to
view the function
that would be
exposed as a web
service
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
29
Create a SoapClient which will consume a
service by using a specified WSDL
require_once('lib/nusoap.php');
$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));
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]);
}
34
Running the 2018WS example (1)
• Download 2018WS from LCMS.
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.