- Knowledge Repositories
- PHP and XML
- Interchanging Data with WDDX
- Summary
Interchanging Data with WDDX
Now that you've learned how to create and process XML documents, it's time to introduce you to a real application of this technology. You've heard that XML is cross-platform, vendor-independent, and supported well by PHP and other programming languages. What about using XML to communicate between different platforms, or between different programming languages? WDDX does exactly that.
The Web Distributed Data eXchange (WDDX)
WDDX is an open technology proposed by Allaire Corporation; it's an XML vocabulary for describing basic and complex data structures such as strings, arrays, and recordsets in a generic fashion, so that they can be moved between different Web scripting platforms using only HTTP. PHP supports WDDX, and so do most other prominent Web scripting languages; for example, Perl, ASP, and ColdFusion.
The Challenge
As Web applications play an ever-increasing role in the software world, one of the most important challenges is data exchange between different programming environments. The new Web promotes distributed, networked applications. First, there were simple Web sites with static information. They grew into more advanced dynamic Web applications, and now these Web applications are beginning to work together, to exchange data, to offer additional, programmable services. It was only recently that open standards like XML emerged and gained widespread use, and that the market forced vendors to adopt these standards in their products.
By extending Web applications to implement open programming interfaces for data exchange and modification, the intelligence of the Web experiences a huge increase. Suddenly, applications can talk to each otherthey evolve from closed, proprietary Web sites to a new generation of wired business applications.
Web applications can expose an API or make data structures available: Data exchanges between distributed servers becomes possible. WDDX offers one possible solution for exchanging structured data. WDDX consists of two parts; one part deals with abstracting data into an XML representation called WDDX packets; the other part translates data structures to and from the scripting environment and WDDX packets.
WDDX is not a remote procedure call facility: There is no way in pure WDDX to invoke a function on a server, which then returns a WDDX packet. How to transfer packets is completely up to you. For this reason, WDDX has no provision for securityit's your task to select a communications channel of appropriate security and allow only authorized parties to access the WDDX part of your system.
Possible Scenarios
Whether for synchronizing data, centralizing data serving, or performing business-to-business communications, structured data exchange opens a new dimension in distributed computing. For example, imagine that you operate an e-commerce Web site. Wouldn't it be great if you could provide customers with package tracking during the shipment? This could be implemented easily if UPS or FedEx provided a way to query for a shipped item programmatically. By opening formed customer-centric services (most couriers do allow tracking packages for their customers) to distributed computing (package tracking as programmable API), a new form of application interaction is made possible.
In another scenario, WDDX could be used in server-to-server interaction. Certain services may work best on a Windows NT platform; for example, querying an SAP back end using a COM connector. You could use ASP for this, and hand over the query results as WDDX packets to your PHP application on a UNIX Web server. There the packets would be turned into PHP data structures, such as arrays, transparently and without any further work.
Abstracting Data with WDDX
The most important fact up front: WDDX is XML. The possible WDDX structures are defined in an XML DTD, and therefore valid WDDX packets are always well-formed and potentially valid XML. The specification doesn't require WDDX packets to be valid, as they don't need to include a document type declaration in the document prologue.
The basic idea is that WDDX lets you transform any standard data structureintegers, strings, arrays, etc.into an XML representation, which conforms to the WDDX DTD. This data can then be transferred over any communications channel capable of transferring XML: HTTP, FTP, email, etc. At any time, the data can be read from the WDDX format back into exactly the same structure as before. During this whole conversion process, datatypes are preserved. If you serialize an array as WDDX packet, it will be an array again at deserialization time.
Now, if this was all, it wouldn't be very exciting. After all, you can serialize any data structure in PHP into a proprietary form using serialize() and bring it back with deserialize() as many times as you like. The interesting aspect of WDDX is that the serialized representation is in XML, and the serializer and deserializer modules are cleanly separated from it. Each of the programming languages supporting WDDX will be able to serialize and deserialize WDDX packets. Serialize a PHP array to a WDDX packet, deserialize the packet in ASPand you've got an array again! The actual implementation of the serializer/deserializer is not important; in PHP, you've got the wddx_*() functions; in ColdFusion, you use the CFWDDX tag; in ASP, a COM component is available.
Sure, all this could be done without WDDX. Doing so would be pretty complicated, though, especially when you think of preserving datatypes across different programming environments. The advantage of WDDX is that it frees you from this undertaking and provides a flexible, open, and pragmatic way for exchanging structured application data.
WDDX Datatypes
Most programming languages share a common set of datatypes; for example, strings or arrays. Accessing these structures may vary from language to language, but the general idea remains: A string is a series of characters, and whether you enclose them with a single quote, double quotes, or not at all doesn't matter from a conceptual point of view.
WDDX supports these generic datatypes. In version 1.0 of the specification, the following types are defined:
-
Null values: null element
-
Boolean values: bool
-
Numbers (with no distinction between integers and floating-point numbers): number
-
Strings: string
-
Date/time values: dateTime
-
Indexed arrays: array
-
Associative arrays: struct
-
Collections of associative arrays, a set of named fields with the same number of rows of data: recordset
-
Binary objects, at this time encoded with Base64: binary
All WDDX packets follow a common XML format. The root element is wddxPacket, with an optional version attribute specifying the version of the WDDX DTD. The version number, currently 1.0, is bound to and defined in the DTD. This means that any WDDX packet with a version of 1.0 is valid only when used with the WDDX DTD 1.0, and not with 2.0 or 0.9.
The header element must follow right below the root wddxPacket element. The header can contain only one comment element, and nothing else.
The next required element is data, denoting the body of the packet. Within this element, exactly one null, boolean, number, dateTime, string, array, struct, recordset, or binary element must appear. A sample WDDX packet is shown in Listing 7.6.
Listing 7.6. A sample WDDX packet.
<wddxPacket version='1.0'> <header/> <data> <struct> <var name='string'> <string>This is a 'string'.</string> </var> <var name='int'> <number>42</number> </var> <var name='float'> <number>42.5</number> </var> <var name='bool'> <boolean value='true'/> </var> <var name='array'> <array length='3'> <number>1</number> <number>2</number> <number>3</number> </array> </var> <var name='hash'> <struct> <var name='foo'> <string>bar</string> </var> <var name='baz'> <string>fubar</string> </var> </struct> </var> </struct> </data> </wddxPacket>
PHP and WDDX
Because WDDX uses XML for the representation of data structures, you need to compile PHP with both XML and WDDX in order to use it. If you've done that, the following functions are at your disposal:
-
wddx_serialize_value()
-
wddx_serialize_vars()
-
wddx_packet_start()
-
wddx_add_vars()
-
wddx_packet_end()
-
wddx_deserialize()
Using these functions, you can serialize PHP variables into WDDX packets and deserialize WDDX packets.
The WDDX Functions
Using the WDDX functions, there are three different ways to construct a packet. The most basic method is to use wddx_serialize_value(). This function takes one variable plus an optional comment, and creates a WDDX packet out of it:
$var = "This is a string."; print(wddx_serialize_value($var, "This is a comment."));
And that's it. This snippet will output the following WDDX packet:
<wddxPacket version='1.0'> <header> <comment>This is a comment.</comment> </header> <data> <string>This is a string.</string> </data> </wddxPacket>
Note: Actually, this packet has been manually edited for clarity: The original packet as created by PHP doesn't contain whitespace or indentation.
The two other methods serialize multiple PHP variables into a WDDX struct element, similar to the one shown in Listing 7.6. The wddx_serialize_vars() function takes an arbitrary number of string arguments containing the names of PHP variables. The function's return value is the WDDX packets as string. The advantage of this is that it lets you serialize multiple PHP variables into one WDDX packet; but note that when fed to the deserializer, it will result in an associative array (of coursethe original distinct PHP variable has earlier been converted to a WDDX struct tag). In code, a basic example for wddx_serialize_vars() could look like the following snippet:
$string = "This is a string."; $int = 42; print(wddx_serialize_vars("string", "int"));
The trio wddx_packet_start(), wddx_add_vars(), and wddx_packet_end() works in basically the same way: Multiple PHP variables are transformed into a WDDX struct as well. The difference is that it works as a transaction with three steps, having the advantage that you can add PHP variables to a WDDX packet over a longer runfor example, during a complex calculation. By contrast, the function wddx_serialize_vars() works in an atomic way. You start assembling a packet by calling the wddx_packet_start() function, which takes one optional argument, the header comment for the WDDX packet. This function returns a packet identifier, similar to file identifiers returned with fopen(). The identifier is used as the first argument in the wddx_add_vars() function. The remaining arguments are exactly the same as with wddx_serialize_vars(): an arbitrary number of strings, containing the names of PHP variables. A basic example:
$i1 = $i2 = $i3 = "Foo"; $packet_id = wddx_packet_start("This is a comment"); for($i=1; $i<=3; $i++) { wddx_add_vars($packet_id, "i$i"); } print(wddx_packet_end($packet_id));
The example simply adds three string variables to a WDDX packet in a for() loop and produces the following output (again, edited for clarity by adding proper indentation):
<wddxPacket version='1.0'> <header> <comment>This is a comment</comment> <data> <struct> <var name='i1'> <string>Foo</string> </var> <var name='i2'> <string>Foo</string> </var> <var name='i3'> <string>Foo</string> </var> </struct> </data> </wddxPacket>