12.10. Exchanging Data with WDDX

Problem

You want to serialize data in WDDX format for transmission or unserialize WDDX data you’ve received. This allows you to communicate with anyone who speaks WDDX.

Solution

Use PHP’s WDDX extension. Serialize multiple variables using wddx_serialize_vars( ):

$a = 'string data';
$b = 123;
$c = 'rye';
$d = 'pastrami';
$array = array('c', 'd');

$wddx = wddx_serialize_vars('a', 'b', $array);

You can also start the WDDX packet with wddx_packet_start( ) and add data as it arrives with wddx_add_vars( ):

$wddx = wddx_packet_start('Some of my favorite things');

// loop through data
while ($array = mysql_fetch_array($r)) {
    $thing = $array['thing'];
    wddx_add_vars($wddx, 'thing');
}

$wddx = wddx_packet_end($wddx);

Use wddx_deserialize( ) to deserialize data:

// $wddx holds a WDDX packet
$vars = wddx_deserialize($wddx);

Discussion

WDDX stands for Web Distributed Data eXchange and was one of the first XML formats to share information in a language-neutral fashion. Invented by the company behind ColdFusion, WDDX gained a lot of popularity in 1999, but doesn’t have much momentum at the present.

Instead, many people have begun to use SOAP as a replacement for WDDX. But WDDX does have the advantage of simplicity, so if the information you’re exchanging is basic, WDDX may be a good choice. Also, due to its origins, it’s very easy to read and write WDDX packets in ColdFusion, so if you need to communicate with a ColdFusion application, WDDX is helpful.

WDDX requires the ...

Get PHP Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.