Home > Articles > Programming > PHP

This chapter is from the book

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 other—they 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 security—it'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 structure—integers, 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 ASP—and 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 course—the 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 run—for 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>

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020