DEV01 - Using XML Services for Web Application Development
Christian Langanke for netlabs.org
Overview
- XML Basics and Features
- Using XML within applications
- Benefits of XML within applications
- Access Web Services with SOAP
- Sample XML service
- Sample Web Application Layout
- Local Result Cacheing
Solution Demo
The Web Application
s
XML Basics and Features
- Data Language for tranferring data between computer systems
- Meta Language for creation of application specific languages
- Human readable data interface
- Mixup of several XML languages possible by using distinct namespaces
Automated syntax and data validation available by DTD or XML Schema
Easy XHTML Generation out of XML data with XSL Schema templates
Using XML within applications
- Create own XML language for your application
- is not visible outside your application
- can be modified or extended at any time later for new requirements
- Create XML data with XMLWriter PHP class
- Pass either XML object or XML data string between application modules
- Generate XHTML output by XSLTProcessor PHP class
- Use XSL templates to form XHTML output per use case
- Use shared templates for covering similar parts of different use cases
Benefits of XML within applications
- Generic data interface between application modules
- Well-defined, human-readable data interface, very useful in debugging
- No validation required, as data does not leave the application
- Own XML language easy to maintain and extend
- XML data can be dumped to web page for debug purposes
- Backend changes are made to code generating the XML data
- Frontend changes are made to the XSL template
Solution Demo
Internal usage of XML
Access Web Services with SOAP
- Simple Framework to invoke object methods
- SOAP XML Language defines data envelopes
- Machine readable data interface definition for
- Service location
- Parameter and return value type
- No direct XML programming required with PHP
- Language native interface available, returns PHP object tree
Sample XML service
- openligadb.de provides arbitrary league data
- Service provider supplies
- A database
- The XML Service interface
- A website with web forms to maintain league data
- Registered users can create any desired league or tournament data and maintain matchday and match data
- Everybody can request any league data in the system
- Most of interest: matchdata of 1. Fußball Bundesliga
- Match results usually available 10-20 mins after end of match
- Service access is not charged, service provider asks for sponsoring
Sample Web Application Layout
- PHP class OpenLigaDB
- Calls methods of the OpenligaDB Service (1-1 method implementation, where applicable)
- Returns PHP object trees, returned by SOAP interface
- Performs all SOAP handling
- Implements local cacheing for performance/availability reasons
- PHP class LeagueData
- Calls methods of OpenLigaDB class, passes XSL Template filename
- Returns XHTML string
- Includes all required result calculation
- Matchday overview, tip player ranking, league season table
- Creation of PDF documents (calls additional Print Framework Class)
Local Result Cacheing
- implement cacheing for
- increasing performance
- providing at least access to cached data
when service is not available
- unfortunately no cache synchronization method implemented in
SOAP protocol (like e.g. in HTTP)
- in service access class implement internal/private cache
methods, like e.g. readcache() and
writecache()
- needs own way of determination if a certain
piece of result data is outdated or not per access method
Ways of determining cache expiration
- guess expiration date according to the expected data modification scheme
- The team data for a season will not change after start of session
- determine expiration date according to data from the database
- The matchday data will not change before start of the next match
- expiration date is part of the data of each service method
- not implemented in OpenLigaDB
- determine expiration date from additinal service method
- OpenLigaDB provides new methods
- GetLastChangeDateByLeagueSaison
- GetLastChangeDateByGroupLeagueSaison
Writing the Local Cache - Tasks
- serialize PHP object tree to a string
- write sring and timestamp to cache
Writing the Local Cache - Sample
Example function for writing cache to a local file:
private function _writecache( $expiredate, $name,
$parm1, $parm2, $parm3,
$result)
{
$cachefilename =
$this->_getcachefilename( $name, $parm1,
$parm2, $parm3);
$fh = fopen( $cachefilename, 'w');
if (!$fh) return false;
fputs( $fh, $expiredate);
fputs( $fh, "\n");
fputs( $fh, serialize( $result));
fclose( $fh);
return true;
}
Reading the Local Cache - Tasks
- read cached content and timestamp
- unserialize the content to a PHP object tree
- if cache is outdated, return nothing
(allows caller to call the service instead)
- if cache is not outdated, return the cached object tree
Reading the Local Cache - Sample
Example function for reading cache from a local file:
private function _readcache( $name, $parm1,
$parm2, $parm3)
{
$cachefilename =
$this->_getcachefilename( $name, $parm1,
$parm2, $parm3);
$fh = fopen( $cachefilename, 'r');
if (!$fh) return false;
$expiredate = trim( fgets( $fh));
$result = unserialize( trim (fgets( $fh)));
fclose( $fh);
$expiretime = strtotime( $expiredate);
if (time() > $expiretime)
return false;
return $result;
}
Using the Local Cache
In each service access method call
- readcache()
before calling the external
service, to determine if there is valid cache data available
- writecache()
after having received new result data
from the external service
Using the Local Cache
Example service access method calling internal cache methods:
public function GetNextMatch()
{
$response = $this->_readcache( "GetNextMatch",
false, false, false);
if (!$response)
{
$params = new stdClass;
$params->leagueShortcut = $this->LEAGUESHORTCUT;
$response = $this->SOAPclient->GetNextMatch( $params);
$dateexpire =
$response->GetNextMatchResult->matchDateTime;
$this->_writecache( $dateexpire, "GetNextMatch",
false, false, false, $response);
}
return $result;
}
Solution Demo
Cache Usage
Session Close
Questions & Answers
The End
Thank you very much for your your attention!