Chapter 7: PHP and Web Distributed Data Exchange


 Download CD Content

Web Distributed Data eXchange (WDDX) is an eXtensible Markup Language (XML)-based technology that helps exchange of data between Web programming languages, such as Hypertext Pre-Processor (PHP), Java, Active Server Pages (ASP), and Perl. WDDX provides a standard format for creating XML-based data structures, which can be easily transmitted across the Internet.

The WDDX module in PHP contains certain functions that convert PHP data to WDDX packets and WDDX packets to PHP data. WDDX functions use WDDX data structures to support information transfer between Web applications. These data structures are platform-independent.

The WDDX Application Programming Interface (API) in PHP supports encoding and decoding functions. Encoding functions convert PHP data to WDDX packets. Decoding functions convert WDDX packets to PHP data.

This chapter describes how to encode and decode data using the WDDX module in PHP. It also explains the data types that WDDX supports.

Introducing WDDX

Using WDDX, you can represent data corresponding to a specific programming language in a language independent format using a process called serialization. Serialization is a process that converts language-specific data structure into a WDDX packet. Deserialization converts a WDDX packet to language-specific data structure. The application that accepts data in the form of WDDX packets uses the deserialization process to retrieve data in its original form.

Understanding WDDX

WDDX API for programming languages provides functions that automatically encode data structures specific to a language into WDDX packets. The WDDX API also provides functions to decode the WDDX packets into language-specific data structures. WDDX packets store data in the XML format. A WDDX packet is created when a Web application converts language-specific data structure into WDDX.

The two important applications of WDDX are server-to-browser data exchange and server-to-server data exchange. In the server-to-browser data exchange, you can retrieve data from an application server, convert data into the WDDX packets using the WDDX API, and transfer the data to the browser in the form of an HyperText Transfer protocol (HTTP) response. In the server-to-server data exchange, the two servers that perform data exchange should have the WDDX API, to map data in a WDDX packet to the corresponding language-specific data structure.

The WDDX API consists of two parts , the WDDX Document Type Definition (DTD) and a set of serialization/deserialization modules. WDDX DTD is a specification that defines WDDX structures. The WDDX serialization/deserialization modules handle translation of data structures specific to a language to platform-independent XML representation or WDDX packets. You can develop WDDX serialization/deserialization modules in programming languages, such as PHP, Perl, ASP, Java, Python, and JavaScript.

WDDX DTD validates a WDDX packet that consists of data stored in the XML format. In the XML format, you enclose the data within the opening and closing tags, similar to HTML. WDDX supports data types similar to the data types in Web programming languages, such as Cold Fusion Markup Language (CFML) and PHP. Data types that WDDX supports are:

  • Numbers: Represent floating-point values. Numbers range from +/-1.7E to +/-308.

  • Date-time values: Represent date and time values that are encoded according to the International Standardization Organization 8601 (ISO8601) standard. You need not prefix 0 for the single digit values of month, day, hour , minute, or second.

  • Strings: Contain arbitrary number of characters within it. A string data type should not contain null values. You can encode a string value using double-byte characters .

  • Binary: Represents strings of binary data. The binary data is encoded in Multipurpose Internet Mail Extensions (MIME) base64 format.

  • Array: Represents a collection of objects. It is an integer index data type.

  • Structure: Represents a collection of objects of arbitrary data type that are indexed using strings.

  • Recordset: Represents encapsulated data in a tabular form. A recordset is a collection of rows, in which each row consists of a set of named fields or columns . You can store only simple data types in a recordset. For storing complex data types in the tabular form, you should use an array of structures. The field names in a table should be in the [_.0-9A-Za-z]+ form, where the character, ., represents a period.

Note  

WDDX supports the null data type. You cannot associate null values with a number or a string.

Installing WDDX

You need to install the expat library available with Apache 1.3.7 or its higher version to use WDDX. The expat library is a C code library that implements a Simple Application Programming Interface for XML (SAX) parser. To install the expat library:

  1. Download the expat package from the following URL:

    http:// sourceforge .net/projects/expat/.

  1. Unzip the package using the following command:

     tar -xvfz expat-1.95.5.tar.gz 
  1. Type the following command to change your working directory to expat-1.95.5:

     pushd expat-1.95.5 

After you install the expat library, recompile PHP by adding -enable-wddx option to the configuration script.

Note  

The windows version of PHP has built-in support for the WDDX functions.

To check the installation of WDDX, run any script that uses WDDX functions provided in PHP. The following code uses a WDDX function:

 <?php print wddx_serialize_value("WDDX example"); ?> 

The above code produces a WDDX packet that contains the string, WDDX example, in the header tags. This WDDX packet ensures that the WDDX functions are already installed on your computer. If the code does not produce any output, install WDDX on your computer.

Exchanging Data using WDDX

WDDX is based on XML 1.0 version, and uses protocols, such as HTTP, to transfer data between Web applications. You can encode and decode data in the form of WDDX packets using serialization/deserialization functions.

Figure 7-1 shows the process of encoding data to the WDDX format and decoding data from the WDDX format:

click to expand: this figure shows the mechanism to convert data into the wddx format and reconvert it to its original form.
Figure 7-1: Data Exchange Using WDDX Packet

The serialization process takes a part of data and converts it into a WDDX packet. Data in the WDDX packet is either a simple data type or complex data type. The simple data types include string, numeric, and Boolean data. The complex data types are arrays, structures, and recordsets. The deserialization process converts data in a WDDX packet to its original data type.

The syntax for the structure that holds data contained in a WDDX packet is:

 <wddxPacket version=1.0> <header></header> <data> <data type>n</data type> </data> </wddxPacket> 

In the above syntax, the <data /> tag pair denotes the start and end of serialized data. The <header /> tag contains a comment. The value n enclosed in the <data type /> tag represents a value that corresponds to the specified data type.

You need to enclose the WDDX packet in the <wddxPacket /> tag pair. The <wddxPacket> tag need to include the current version of WDDX. The data types that you can serialize into a WDDX format are: <number>, <Boolean>, <array>, <struct>, and <binary>. The following code shows the WDDX packet that contains a serialized string value:

 <wddxPacket version=1.0> <header></header> <data> <string>This is a WDDX packet</string> </data> </wddxPacket> 

The above WDDX packet contains a string value, and the application stores the value inside the tags as a string. If an application needs to deserialize the WDDX packet, it checks the <data type> tag pair to uncover the type of data stored in the packet.

Note  

In a WDDX packet, the application ignores the space between any pair of tags.




Integrating PHP and XML 2004
Integrating PHP and XML 2004
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 51

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net