Download as pdf or txt
Download as pdf or txt
You are on page 1of 26

Lecture 22: Web Services

Internet Technologies and Web


Services – CSE2034Y
Agenda
• Creating a web service in PHP
• Using the web service results to generate an
XML file and display using XSLT.
3

1. Server.php
4

Server.php (1)
The purpose of the Server.php is:
1. To create a SoapServer:
$server = new soap_server();

2. Provide the web service.


• Initialise WSDL Support
• Declare the WSDL types and methods
• Implement the method/s
• Register the method/s
• Process the request

Recall since we are using NuSoap, WSDL does not need to be


written but is generated for us.
5

Server.php (2)
• We set a namespace for the web service to make it
unique and identifiable. The namespace does not have
to be a URL but if you make it a URL things can be a lot
easier.
$namespace = "http://www.uom.ac.mu/experience”
• Next we start to program against the server object:
$NAMESPACE =
'http://www.uom.ac.mu/experience';
$server->debug_flag=false;
$server->configureWSDL('Module', $NAMESPACE);
$server->wsdl->schemaTargetNamespace =
$NAMESPACE;
6

Server.php (3)
• In this case configureWSDL contains two parameters.
The first defines the web service name, the second
parameter contains the namespace.

$server->configureWSDL('Module', $NAMESPACE);
7

Data Types
• There are different types of data that can be used.
• Built in types can be used without being declared.
• Structures and arrays are more complicated.
$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')
)
);
8

Data Types(2)
This is quite a complicated function, and from the source
code we see that the parameters are as follows:
1: name
The first is simply the name you want to define (in the
context of the ‘targetNamespace’ defined above.
2: typeClass (complexType|simpleType|attribute)
The next is the type. simpleType is usually based on
another type with restrictions (e.g. a string with max
length of 4 or some such other restriction), complexTypes
usually have more than one subelement (as above).
3: phpType: currently supported are array and
struct (php assoc array)
The phpType, as specified is either array or struct.
9

Data Types(3)
4: compositor (all|sequence|choice)
The compositor is a way of defining how the subelements
of the current element work, are they either all optional
element, to be included in any order (all), optional but
only to be allowed in the given order (sequence) or only
one of them is allowed at any one time (choice).

5: restrictionBase namespace:name
(http://schemas.xmlsoap.org/soap/encoding/:Arr
ay)
The restrictionBase and attrs and arrayType are needed
for arrays support.
10

Data Types(4)
6: elements = array ( name =
array(name=>”,type=>”) )
The elements item is an array containing a list of
subelement, each of which has a name and a type.
7: attrs = array(array(‘ref’ =>
“http://schemas.xmlsoap.org/soap/encoding/:arr
ayType”,
“http://schemas.xmlsoap.org/wsdl/:arrayType”
=> “string[]”))
8: arrayType: namespace:name
(http://www.w3.org/2001/XMLSchema:string)
11

Data Types(5)
• The above example defines a type called
“'LecturerExperience” which is effectively a struct,
containing 3 fields, Lecturer, Module, and Experience.
• It is a complex type and all elements should be included
in that specific order. Lecturer and Module are strings
and Experience is an integer.
12

Data Types(6)
• The next thing we want is to return a list of lecturers
based on the search criterion. For this we use the array
data type:
$server->wsdl->addComplexType(
'LecturerExperienceArray',
'complexType',
'array',
'',
'SOAP-ENC:Array',
array(),
array(
array('ref'=>'SOAP-ENC:arrayType',
'wsdl:arrayType'=>'tns:LecturerExperience[]')
),
'tns:LecturerExperience'
);
13

Data Types(7)
• Here the php type is array instead of struct.
• The compositor is blank and the restrictionBase is
‘SOAP-ENC:Array’.
• This tells the system that your complex type is based on
the Array type defined within the soap-encoding
namespace.
• The elements parameter is then a blank array and the
attrs parameter is an array defining the type of the
array and the parameter defines the type of the
elements within the array (as you can see, these are
usually the same except for the [] part after the array
definition.
14

Getting the web service to do


work
• So far we’ve defined the types of data that we want to
pass in and out of our web service.
• Next we define the actual operation the web service will
support.
$server->register('getExperience', // method name
array('input' => 'xsd:string'), // input parameters
array('output' => 'tns:LecturerExperienceArray'),
// output parameters
$NAMESPACE);
15

Getting the web service to do


work - A more complete example
$server->register('GetWalk', // method name
array('WalkId' => 'xsd:int'), // input parameters
array('return' => 'tns:Walk'), // output parameters
$namespace, // namespace
$namespace . '#GetWalk', // soapaction
'rpc', // style
'encoded', // use
'Get Specific Walk' // documentation
);
16

Server.php (4)
• Finally, we pass whatever data the php page receives
into the soap processor and see what happens:
• Use the request to (try to) invoke the service

$HTTP_RAW_POST_DATA =
isset($GLOBALS['HTTP_RAW_POST_DATA']) ?
$GLOBALS['HTTP_RAW_POST_DATA'] : ' ';
$server->service($HTTP_RAW_POST_DATA);
exit();
17

WSDL format and Response construction

WSDL format
<we:LecturerExperience>
<we:LecturerName>STRING</we:LecturerName>
<we:Module>STRING</we:Module>
<we:Experience>INTEGER</we:Experience>
</we:LecturerExperience>

$lecturerExperience = array(); Response Construction


$result = array();
if(mysql_num_rows($dbResult) > 0){
while ($row = mysql_fetch_assoc($dbResult)) {
$lecturerExperience[] = array('LecturerName'=>$row['Name'], 'Module' => $row['Module'],
'Experience' => $row['Experience']);
}
mysql_free_result($dbResult);
$result=array('LecturerExperience'=>$lecturerExperience);
}
return $result;
18

2. Client.php

30
19

Client.php (1)
The purpose of the client.php is:
1. Capture values from page/url if any
if(isset($_GET["txt_module"]))
$module = $_GET["txt_module"];
2. Create a soap client - Initialise nusoap_client and tell it to
accept a php/ WSDL file.
$url = "http://localhost/2013WSEx1/server.php?wsdl";
$client = new nusoap_client($url);
3. The getError method checks if their were any errors whilst creating
the client.
$err = $client->getError();
if ($err) {
echo '<p><b>Error: ' . $err . '</b></p>';
}
20

Client.php (2)
4. Call the method in the web service with arguments
$args = array('input' => $module);
$response = $client->call('getExperience', array($args));
The call method defines the RPC method and arguments
and sends the SOAP request.
5. Errors can be checked for again
6. The next few lines of code are used to show the
SOAP request and response (ideal for debugging)
echo '<h2>Request</h2>';
echo '<pre>' . htmlspecialchars($client->request,
ENT_QUOTES) . '</pre>';
echo '<h2>Response</h2>';
echo '<pre>' . htmlspecialchars($client->response,
ENT_QUOTES) . '</pre>';
21

Client.php (3)
7. Display the results
//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']);
}
22

Client.php (4)
8. 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);
23

UDDI
Universal Description, Discovery, and Integration
24

Conception
• As stated before, UDDI was a master directory
meant to advertise web services; a sort of yellow
pages of web services.
• The adoption of UDDI never reached the scale
originally intended, so companies either shut
down or privatized their registries.
• But the technology is not dead; W3.org is drafting
version 3.0 of the UDDI specification. So in the
future, it might become functional again.
25

How to find web services then?


• You may Google for web services.
Classwork
• Write the code for the following:
▫ An interface where user can enter studentID and a ‘Get
Modules’ button. The info is submitted to client.php.
▫ The client.php page which will get the list of registered
modules for the student for the current semester.
▫ The server.php which provides the web service facility
and has a function getRegistration.
▫ The database name is Modules. The table name is
‘StudentModuleRegistration’ and has fields: studentID,
moduleCode, semester and year.

You might also like