Enabling the XMLWriter Extension
As of PHP 5.1.2, XMLWriter is included with the core PHP distribution and enabled by default. Prior to this version, XMLWriter was available only through the PECL at http pecl.php.net package xmlwriter. I will explain both types of installation. PECL is similar to PEAR, explained in Chapter 13, except rather than a collection of software and classes written in PHP PECL is a collection of extensions to PHP written in C. These extensions work in the same manner as those you already use when...
Tip Remember to set the ContentType header to textxml prior to returning the
Putting this all together, you can create a server to service the request from the client created in the previous section. It defines two functions, buy_stock and sell_stock , that are registered with an XML-RPC server. The only two stocks, defined in the arStocks array, that can be used within these functions are Yahoo YHOO and Google GOOG . The following is the complete code for the server, referenced as the file stocktrader.php by the client. I wrote it to run within a Web server because it...
XMLSerializer Class
The XML_Serializer class transforms your data into XML documents. It can operate in one of two modes. The difference between modes is how indexed arrays are handled. Indexed arrays are arrays that use numeric keys. Two possible modes exist, as shown in Table 13-4. simplexml XML_SERIALIZER_MODE_SIMPLEXML When serializing indexed arrays, the default tag is used as the tag name. When serializing indexed arrays, the key of the parent value is used as a tag name. To illustrate the difference between...
XMLSerializer Package
XML_Serializer provides the ability to quickly and easily transform arrays and objects into XML documents, as well as the other way around. It depends upon the XML_Parser and XML_Util packages, so make sure these are installed before trying to use this package. The package consists of two classes, XML_Serializer, which performs the serialization of a data structure, and XML_Unserializer, which takes an XML document and returns an array or objects based on the contents. Note This package is...
Stream Context
Stream contexts are parameters and options that can modify the behavior of a stream. Many of the stream-enabled functions within PHP accept a stream context as a parameter. The XML functions are not included in this because stream usage is almost invisible from an API perspective. The libxml extension includes the function libxml_set_streams_context that you can use for this purpose. You can create a context with the regular PHP Streams API. You can find full documentation for this API in the...
NodeList
A NodeList is a collection of ordered nodes accessed by index starting at position 0. An object of this type is often returned from DOM methods that can return more than a single node. It is important to know that these objects are live. In simple terms, modifications within the document tree are reflected in these objects. For example, if you had an instance of a NodeList object containing the children of a certain element, all changes to the children would be reflected in the instantiated...
NameNodeMap
A NameNodeMap is similar to a NodeList, except in that the collection can be accessed via item name as well as via index. The difference in the indexing is that these objects have no specific ordering for the objects they contain because the most important aspects of the contained objects are the names. These collections are also live, so the same issues surrounding a NodeList are applicable to a NameNodeMap. You will see plenty of examples of this throughout this chapter. Caution NodeList and...
Character Data Handler
Character data events are issued when text content, CDATA sections, and in certain cases entities are encountered in the XML stream. Text content is strictly text content within an element in this case. It differs from the conventional text node when the document is viewed as a tree because text nodes can live as children of other nodes, such as comment nodes and PI nodes. You can set a character data handler using the xml_set_character_data_handler function. Its prototype is as follows bool...
Adding SOAP Headers
The SOAR extension added a simple method to add SOAR header entities in RHR 5.0.5. If you're running any version prior to this, you must set headers using other mechanisms described in the section Low-Level Calls. In all likelihood, you are probably running a newer version of RHR which means you have access to the_setSoapHeaders method. This method sets headers for the lifetime of a client, which means once set using this method, subsequent calls made by the client will add the headers to the...
SoapParam
The SoapParam class provides the ability to create named parameters. Like the other common classes, the SoapParam class only has a constructor that simply takes the data to be associated with a name _construct mixed data, string name The data parameter accepts a value containing either a native PHP type or a SoapVar. The name parameter accepts a string containing the name to be used for the parameter. Using the PeopleStruct object, created earlier in the SoapVar section, you can create a...
Introducing UDDI
In 2000, Microsoft, IBM, and Ariba collaborated on a project to create standards for describing, discovering, and consuming Web services. The idea was for registries, known as UDDI registries, to be set up to manage information about service providers, service implementations, and service metadata. Providers, typically businesses, could then publish and maintain their information while giving consumers, consisting of anyone needing to consume a service, the ability to query the information to...
Using ServicesYahooContentAnalysis
The Services_Yahoo_ContentAnalysis class is also a central point to create the correct object needed to perform spelling suggestions and term extractions. You can include this class like so require_once Just like with the Services_Yahoo_Search class, you use the factory method to create the appropriate class for the Web service. The acceptable values that can be passed in this case are as follows termExtraction Accesses the Spelling Suggestion service to get a suggested spelling correction for...
XML Output Method
When the result tree is output as XML, the processor creates a well-formed tree or issues errors when this is not possible. It is important that any literal tags you may use within templates are written as well-formed XML as well. For instance, creating the start tag for an element, yet never closing, it would cause an error lt -- This is an error because it does not produce well-formed XML -- gt lt xml version 1.0 lt xsl stylesheet version 1.0 gt lt xsl output method xml gt lt xsl template...
Introducing XQuery XPath and XSLT
XPath 2.0 is the new generation of XPath. It serves as the foundation for XQuery and XSLT 2.0. These technologies are still in the working draft phase from the W3C. You can find the specifications at http www .w3.org TR xpath20 , http www.w3.org XML Query, and http www.w3.org TR xslt20 . This section will introduce you to XPath 2.0. XQuery is almost synonymous with XPath 2.0 at this point, but XSLT 2.0 is out of the scope of this book. Although some of the larger database vendors support...
SOAP mustUnderstand
The mustUnderstand attribute provides some direction to the recipient of a header entry about what to do if it does not understand or cannot fully and correctly process the entry. This attribute takes the value 0, which is the default when not specified, or 1. When the value 1 is used and the recipient is unable to fully process the header, it is instructed to fail processing the message and issue a SOAR fault. When the attribute is omitted or its value is set to 0, the application can ignore...
Exploring the XMLRPC Structure
Structure within XML-RPC depends upon the direction of the data. Though the two share many aspects, a request takes a different form than a response. Not only does each have its own XML structure, but also each requires specific information in the HTTP headers. The following sections will cover each of these aspects and provide examples of the structure used. Before getting into any specific area, it is first important to know and understand some of the common XML elements shared between the...
Element Events
Two events occur with elements within a document. The first event occurs when the parser encounters an opening element tag, and the second occurs when the closing element tag is encountered. Handlers for both of these are registered at the same time using the xml_set_element_handler function. This function takes three parameters the parser resource, a string identifying the start element handler function, and a string identifying the end element handler function. The function set for the start...
Using DOM and SimpleXML Together
DOM and SimpleXML are able to work together on the same document at the same time. Nodes can be imported from one extension to the other, which means the node can be shared between the extensions without being a copy. Actions performed on the node from one extension are reflected within the object from the other extension. DOM imports nodes from SimpleXML using the dom_import_simplexml function, and SimpleXML imports nodes from DOM using the simplexml_import_dom function. The majority of times...
Creating the Client
Create a SOAR client by instantiating a SoapClient object. It is at this point you can set a number of options as well as whether WSDL will be used _construct mixed wsdl , array options The wsdl parameter is an important parameter. It determines whether the client will use WSDL if so, it specifies the location of the WSDL document. When using the client in non-WSDL mode, you must pass NULL. You can pass options, listed in Table 18-6, as an associative array. When working in non-WSDL mode, this...
Encryption Granularity
You can use XML encryption to sign virtually any type of data. This includes both XML and non-XML-based data. Just like XML signatures, the data can even be located outside the XML encryption document. This section will explain the granularity available using XML encryption. This means examining the different pieces and types of data that could be encrypted and their relation to the XML encryption document. Consider the example of an order from an e-commerce site explained earlier. If using XML...
Using xmlsetobject
Other than defining the class, writing the handlers as methods of the class, and registering an instantiated object of this class with the parser, using this API is no different from what you have seen so far. The xml_set_object function takes the parser and the instantiated object to be used for handling events as parameters. Handlers are registered in the same way. Only the name of the function, in this case the method, is set with the handler. Parsing then is performed in a normal fashion,...
Introducing Exclusive XML Canonicalization
An issue faced earlier in the chapter when working with canonical XML dealt with extracting a document subset and inserting it into a different context. This caused many problems because canonical XML includes the document subset's ancestor namespace declarations and attributes within the XML namespace. For instance, a wrapper node encapsulates a subset and might be used for something like transport. If you are familiar with SOAP you know this would be equivalent to its envelope. The following...
Complex Type Document Literal Call
If you refer to the Conversions service, whose types and functions are shown in Listing 18-10, you will notice that all the functions take complex types for parameters. You can create these complex types in a variety of ways. For example, the following functions take complex types as parameters and return a complex type as the result NumberToDollarsResponse NumberToDollars NumberToDollars parameters TitleCaseWordsResponse TitleCaseWords TitleCaseWords parameters All of these types are structs....
Shopping
Performing a shopping search on Yahoo allows you to search matching products, see specific offers from merchants, view user ratings, and compare products. Through the various shopping Web services that Yahoo provides, it is also possible to perform these actions programmatically. The example in this section will demonstrate how to perform product searches using the Yahoo Product Search service. You can find additional information and documentation for this service at The service itself, which...