Zend PHP5 certification Exam study review 3: XML & Web Service

Following previous review on Streams and Network, here is the XML and Web Service. This is more and more important area in php I think since most Web2.0 applications use XML and Web Service technology. Also, PHP5 comes a new XML extension, SimpleXML. The exam seems requires you know the difference between SAX (Simple API for XML) and SimpleXML extension and DOM extension in PHP. SimpleXML
Parsing XML Documents
The only restraint is that the XML document must be well-formed, or SimpleXML will emit warnings and fail to parse it. Also, while the W3C has published a recommended specification for XML1.1, SimpleXML supports only version 1.0 documents. Again, SimpleXML will emit a warning and fail to parse the document if it encounters an XML document with a version of 1.1.

All objects created by SimpleXML are instances of the SimpleXMLElement class. Thus, when parsing a document or XML string, you will need to create a new SimpleXMLElement
object simplexml_load_string ( string $data [, string $class_name [, int $options [, string $ns [, bool $is_prefix]]]] )
Interprets a string of XML into an object
Takes a well-formed XML string and returns it as an object. pairing file_get_contents() with simplexml_load_string()

however, in a real-world scenario, it would make much more sense to simply use simple_xml_load_file():
object simplexml_load_file ( string $filename [, string $class_name [, int $options [, string $ns [, bool $is_prefix]]]] )

SimpleXMLElement class
// Load an XML file
$library = new SimpleXMLElement(’library.xml’, NULL, true);
Note here that the second method also passes two additional arguments to SimpleXMLElement’s constructor. The second argument optionally allows the ability to specify additional libxml parameters that influence the way the library parses the XML. It is not necessary to set any of these parameters at this point, so we left it to NULL. The third parameter is important, though, because it informs the constructor that the first argument represents the path to a file, rather than a string that contains the XML data itself.

SimpleXMLElement::xpath()
SimpleXMLElement::addChild()
SimpleXMLElement::addAttribute()

asXML() method of $library SimpleXMLElement.
Called without a parameter, the asXML() method returns an XML string. However asXML() also accepts a file path as a parameter, which will cause it to save the XML document to the given path and return a Boolean value to indicate the operation’s success.

If a file with the same path already exists, a call to asXML() will overwrite it without warning (provided that the user account under which PHP is running has the proper permissions).

It is possible to remove child elements, though, using the following method.
$library->book[0] = NULL;

This only removes child elements and their attributes, however. It will not remove attributes from the element at the book level. Thus, the isbn attribute remains. You may set this attribute to NULL, but doing will only cause it to become empty and will not actually remove it. To effectively remove children and attributes, you must export your SimpleXMLElement to DOM, where this more powerful functionality is possible.

xpath and Namespaces
Xpath: Combines the mojo of Regex with some SQL like results
http://www.w3schools.com/xpath/xpath_syntax.asp
XML Namespaces provide a method to avoid element name conflicts.
When a namespace is defined in the start tag of an element, all child elements with the same prefix are associated with the same namespace.

<h:table xmlns:h=”http://www.w3.org/TR/html4/“>

Note that the address used to identify the namespace is not used by the parser to look up information. The only purpose is to give the namespace a unique name. However, very often companies use the namespace as a pointer to a real Web page containing information about the namespace. Try to go to http://www.w3.org/TR/html4/.
Default Namespaces
Defining a default namespace for an element saves us from using prefixes in all the child elements.

<table xmlns="http://www.w3.org/TR/html4/"> 
   <tr> 
   <td>Apples</td> 
   <td>Bananas</td> 
   </tr> 
</table> 
When you start using XSL, you will soon see namespaces in real use. XSL style sheets are used to 
transform XML documents into other formats, like HTML. 
If you take a close look at the XSL document below, you will see that most of the tags are HTML tags. 
The tags that are not HTML tags have the prefix xsl, identified by the 
namespace "http://www.w3.org/1999/XSL/Transform":
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/"> 
<html> 
<body> 
  <h2>My CD Collection</h2> 
  <table border="1"> 
    <tr> 
      <th align="left">Title</th> 
      <th align="left">Artist</th> 
    </tr> 
    <xsl:for-each select="catalog/cd"> 
    <tr> 
      <td><xsl:value-of select="title"/></td> 
      <td><xsl:value-of select="artist"/></td> 
    </tr> 
    </xsl:for-each> 
  </table> 
</body> 
</html> 
</xsl:template>
</xsl:stylesheet>  

XSL 
It started with XSL and ended up with XSLT, XPath, and XSL-FO.  

DOM
DomDocument
class

$dom = new DomDocument();
$dom->load(“library.xml”);

string version:
$dom = new DomDocument();
$dom->loadXML($xml);

DomDocument::loadHtmlFile() and DomDocument::loadHTML()

DomDocument::save() (to a file) , DomDocument::saveXML() (to a string) , DomDocument::saveHTML() (also to a string, but saves an HTML document instead of an XML file), and DomDocument:saveHTMLFile() (to a file in HTML format).

XPath Queries
One of the most powerful parts of the DOM extension, is its integration with XPath—in fact, Dom Xpath is far more powerful than the SimpleXML equivalent.

$dom = new DomDocument();
$dom->load(“library.xml”);
$xpath = new DomXPath($dom);
$xpath->registerNamespace(“lib”, “http://example.org/library“);
$results = $xpath->query(“//lib:title/text()”);
foreach ($results as $book) {
echo $book->data;
}

A call to DomXpath::query() will return a DomNodeList object; you can find out how many items it contains by using the length property, and then access any one of them with the item() method. You can also iterate through the entire collection using a foreach() loop.

DomNode
DomDocument::createElement(), DomDocument::createElementNS(), and DomDocument::createTextNode() methods

$dom = new DomDocument();
$dom->load(“library.xml”);
$book = $dom->createElement(“book”);
$book->setAttribute(“meta:isbn”, “0973589825”);
$title = $dom->createElement(“title”);
$text = $dom->createTextNode(“php|architect’s Guide to PHP Design Patterns”);
$title->appendChild($text);
$book->appendChild($title);
$author = $dom->createElement(“author”,”Jason E. Sweat”);
$book->appendChild($author);
$publisher = $dom->createElement(“pub:publisher”, “Marco Tabini & Associates, Inc.”);
$book->appendChild($publisher);
$dom->documentElement->appendChild($book);

DomNode::appendChild() and DomNode::insertBefore()
DomNode::appendChild() and DomNode::insertBefore() will move the node to the new location. If you wish to duplicate a node, use “DomNode::cloneNode()” first.

Modifying Data

$xml = <<<XML
<xml>
<text>some text here</text>
</xml>
XML;
$dom = new DOMDocument();
$dom->loadXML($xml);
$xpath = new DomXpath($dom);
$node = $xpath->query(“//text/text()”)->item(0);
$node->data = ucwords($node->data);
echo $dom->saveXML();

Removing Data
There are three types of data you may want to remove from an XML document: attributes, elements and CDATA. DOM provides a different method for each of these tasks: DomNode::removeAttribute(), DomNode::removeChild() and DomCharacterData::deleteData().

WorkingWith Namespaces
DOM is more than capable to handle namespaces on its own and, typically, you can, for the most part, ignore them and pass attribute and element names with the appropriate prefix directly to most DOM functions.

$dom = new DomDocument();
$node = $dom->createElement(’ns1:somenode’);
$node->setAttribute(’ns2:someattribute’, ’somevalue’);
$node2 = $dom->createElement(’ns3:anothernode’);
$node->appendChild($node2);
// Set xmlns:* attributes
$node->setAttribute(’xmlns:ns1’, ’http://example.org/ns1’);
$node->setAttribute(’xmlns:ns2’, ’http://example.org/ns2’);
$node->setAttribute(’xmlns:ns3’, ’http://example.org/ns3’);
$dom->appendChild($node);
echo $dom->saveXML();

We can try to simplify the use of namespaces somewhat by using the DomDocument::createElementNS() and DomNode::setAttributeNS() methods

$dom = new DomDocument();
$node = $dom->createElementNS(’http://example.org/ns1’, ’ns1:somenode’);
$node->setAttributeNS(’http://example.org/ns2’, ’ns2:someattribute’, ’somevalue’);
$node2 = $dom->createElementNS(’http://example.org/ns3’, ’ns3:anothernode’);
$node3 = $dom->createElementNS(’http://example.org/ns1’, ’ns1:someothernode’);
$node->appendChild($node2);
$node->appendChild($node3);
$dom->appendChild($node);
echo $dom->saveXML();

Interfacing with SimpleXML
You can import SimpleXML objects for use with DOM by using dom_import_simplexml():

$sxml = simplexml_load_file(’library.xml’);
$node = dom_import_simplexml($sxml);
$dom = new DomDocument();
$dom->importNode($node, true);
$dom->appendChild($node);

The opposite is also possible, by using the aptly-named simplexml_import_dom() function:

$dom = new DOMDocument();
$dom->load(’library.xml’);
$sxe = simplexml_import_dom($dom);
echo $sxe->book[0]->title;

SAX and DOM
SAX
– Simple API for XML
. Event based approach where every ‘event’ encountered must be coded for and handled
. Events include open tag, close tag, tag data

DOM
– Document Object Model
.Loads entire document into ram, then creates an internal tree representation

Web Services XML-RPC SOAP REST
PHP 5 contains tools particularly suited for SOAP and REST Web services

SOAP requests are sent using post.
SOAP is intrinsically tied to XML because all messages sent to and from a SOAP server are sent in a SOAP envelope that is an XML wrapper for data read and generated by the SOAP server. Creating the XML for this wrapper can be a tedious process and, therefore, many tools and external PHP libraries have been created to aid developers in the cumbersome process of forming SOAP requests and reading SOAP server responses. PHP 5 simplifies this process with its SOAP extension—which makes the creation of both servers and clients very easy.

WSDL document
This, in turn, is yet another XML document that describes the function calls made available by a Web service, as well as any specialized data types needed by it.

The SoapClient class
It is essentially a one-stop solution to creating a SOAP client—all you really need to do is provide it with the path to a WSDL file, and it will automatically build a PHP-friendly interface that you can call directly from your scripts.

e.g.

try
{
$client = new SoapClient(’http://api.google.com/GoogleSearch.wsdl’);
$results = $client->doGoogleSearch($key, $query, 0, 10, FALSE, ’’, FALSE, ’’, ’’, ’’);
foreach ($results->resultElements as $result)
{
echo ’<a href=”’ . htmlentities($result->URL) . ’”>’;
echo htmlentities($result->title, ENT_COMPAT, ’UTF-8’);
echo ’</a><br/>’;
}
}
catch (SoapFault $e)
{
echo $e->getMessage();
}

$key: Google developer Key.
$query: The search Keyword phrase.

SoapClient uses the WSDL file to construct an object mapped to the methods defined by the web service; thus, $client will now provide the methods doGetCachedPage(), doSpellingSuggestion(), and doGoogleSearch().

The constructor of the SOAPClient class also accepts, as an optional second parameter, an array of options that can alter its behaviour; for example, you can change the way data is encoded, or whether the entire SOAP exchange is to be compressed, and so on. If you are accessing a SOAP service that does not have a WSDL file, it is possible to create a SOAP client in non-WSDL mode by passing a NULL value to the SoapClient constructor instead of the location of the WSDL file. In this case, you will have to pass the URI to the Web service’s entry point as part of the second parameter.

Debugging
$client = new SoapClient(’http://api.google.com/GoogleSearch.wsdl’, array(’trace’ => 1));

SOAP Server
Requests are encapsulated within a SOAP Envelope
Responses are XML documents with similar Envelope & Body sections
When creating a SOAP server, you simply start with a class that contains the methods you wish to make available to the public through a Web service and use it as the basis for a SoapServer instance.

class MySoapServer
{
public function getMessage()
{
return ’Hello, World!’;
}
public function addNumbers($num1, $num2)
{
return $num1 + $num2;
}
}

When creating a SOAP server with SoapServer, you must decide whether your server will operate in WSDL or non-WSDL mode. At present, SoapServer will not automatically generate a WSDL file based on an existing PHP class, although this feature is planned for a future release. For now, you can either create your WSDL files manually—usually an incredibly tedious task, use a tool (like the Zend Studio IDE) that will generate one for you, or choose not to provide one at all.

$options = array(’uri’ => ’http://example.org/soap/server/’);
$server = new SoapServer(NULL, $options);
$server->setClass(’MySoapServer’);
$server->handle();

While this SOAP service will work just fine in non-WSDL mode, it is important to note that aWSDL file can be helpful to both users of the service and to the SoapServer object itself. For users, a WSDL file helps expose the various methods and data types available. For the server, the WSDL file allows the mapping of different WSDL types to PHP classes, thus making handling complex data simpler.

$options = array(
’location’ => ’http://example.org/soap/server/server.php’,
’uri’ => ’http://example.org/soap/server/’
);
$client = new SoapClient(NULL, $options);
echo $client->getMessage() . “\n”;
echo $client->addNumbers(3, 5) . “\n”;

REST:
Requests look like filled out forms, can use either GET or POST
Response is an XML document
Request and Response is defined by the service provider

Representational State Transfer, the focus is on the presence of resources in the system.
Services that use the REST architecture are referred to as RESTful services; those who use or provide RESTful services are sometimes humorously referred to as RESTafarians.
There are a number of RESTful Web services, the most popular of which thrive in the blogosphere. In a loose sense,Web sites that provide RSS and RDF (Resource Description File) feeds provide a RESTful service.
Most RESTful services respond with XML data, and SimpleXML provides the best interface to interact with them.
The popular bookmarking site, del.icio.us, is one example of a Web site providing a REST service that returns XML data ready for SimpleXML to consume.Note that del.icio.us uses HTTP authentication over SSL for its REST URIs; most RESTful services provide some kind of authentication or developer key scheme to gain access to the service.

2 Comments - Leave a comment
  1. […] etc. Recomendación # 2: Visitar el Blog “Read The Web” de  un colega Chino! Clic aquí Aquí hay encontrará unas revisiones de cada tema de la […]

  2. […] 2: Visitar el Blog “Read The Web” de un desarrollador php Chino! Clic aquí Aquí encontrará unas revisiones de cada tema de la […]

Leave a Reply

Your email address will not be published. Required fields are marked *

*