XML, Web Services & PHP

Today I’ll attend two tutorials: XML, Web Services & PHP and PHP Extension Development.

Here are the some notes taken on the XML, Web Services & PHP Tutorial.

Speaker: Christian Wenz- http://www.hauser-wenz.de/s9y/

(cont..)

* XML:

– History of XML:
. 1998: XML 1.0 becomes w3c recommendation.
. 1999: xslt 1.0, xpath 1.0

– DTD (Document Type Definition/Declaration):
. DTD block defines which elements are allowed and which attributes they have.
Ex:

<!ELEMENT sessions (session+) >
<!ELEMENT session (title, speaker+, comments?>

– Schema:
. Proposed by the w3c to be the successor of DTD
. Schema documents are XML documents
. Data type are supported
. Use of namespaces
. Schema inheritance
. etc

but adds complexity.

There are several converters from DTD to Schema.

– XSL(T):
. eXtended Stylesheet Language Transformation
. Language for transforming XML documents in other formats (or in different XML)
. XSLT is powerfull
. XSLT can be very frustrating

* XML in PHP5:

Access XML:

– Expat:
. Open Source XML Parser
. Uses SAX approach
. Bundled with PHP (–with-xml)
. PHP 5: Based on libxml (included)

. “Event- based parser” – Xml is read in as block of data; for each element, events are triggered
. Events available for Expat: start of element, end of element, character data (cdata)
. Not always intuitive, but doable

He also pointed 3 steps to parse XML with expat, but the PHP manual explains this very well.
. To simplify a bit you can use xml_parse_into_struct();

– DOM (Document Object Model)
. Idea: XML is hierarchical; sub-elements are “further down the road”; representation as tree with a root and nodes
. Extension based on libxml in PHP4 (–with-dom), libxml2 in PHP5 (built-in)
. It can consume a lot of memory, because the whole document is in memory while in SAX you only have blokcs of the document in memory.

. Changes in PHP5: OOP access, studlyCaps. Now everything is named after the w3c specification, but existing scripts break.

XSLT with DOM XML in 4 steps:
1. Read in XML
$xml = new DOMDocument()
$xml->load("sessions.xml");

2. Read in XSL
$xsl = new DOMDocument()
$xsl->load("sessions.xsl");

3 . Transform
$p = new XSLTProcessor();
$p->importStyleSheet($xsl)

4. Print results
$result = $p->transformToXML($xml);

– Validating XML: validate() (against DTD), schemaValidate(), relaxNGValidate();

– SimpleXML
. KISS
. Load XML and have object oriented access to data
. Is part of PHP5

. It can be used with DOM

Samples:
. He presented some samples using SimpleXML to build XML from reading EXIF from images

. XMLReader (.NET) / PHP 5.1
. XMLWritter:
. Fast writting XML
. Forwarding only
. Not default in PHP5.1

SVG: XML to represent Vector Graphics. Firefox 1.5 supports it but not completely. Widely used in Asia for Cell Phones.

* WEBSERVICES:
. idea: Machine 2 machine communication
. to be honest: no new idea
. but: Now it’s standardized
. big hype, thanks to Microsoft and IBM
. .NET makes Web Services very easy
. But: PHP closes the gap, step by step

Currently there are 14 PEAR packages (soon 15)

– REST (Representational State Transfer)
. based on resources accessible via URL
. based on 2000 dissertation by Roy Fielding (co-author of the HTTP specification)

– Buzzword bingo:
. XML-RPC . UDDI
. SOAP . DISCO
. WSDL . REST

80% of Amazon Webservices users use REST

– XML-RPC:
. Designed by Dave Winer from UserLand
. MS helped w/specification but stayed in the background
. Idea: Remote Procedure call via XML and HTTP
. XML-RPC requires PEAR

– SOAP:
. Simple Object Access Protocol
. but: neither simple nor has anything to do with access protocol

. Packages:
– SOAPx4: The “father” of all
– NuSOAP
– PEAR::SOAP: also rewrite of SOAPx4e, actively maintained
– ext/SOAP: official C extension, included in PHP5

– WSDL:
. one more acronym
. offers description for a web service
. veeery complex
. .NET does this automagically
. Nowadays PHP can do that too, but not PHP Soap. Use NuSOAP or PEAR::SOAP.