12.11. Exchanging Data with WDDX12.11.1. ProblemYou 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. 12.11.2. SolutionUse 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); 12.11.3. DiscussionWDDX 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 expat library, available with Apache 1.3.7 and higher or from http://www.jclark.com/xml/expat.html. Configure PHP with --with-xml and --enable-wddx. The example in the Solution produces the following XML (formatted to be easier to read): <wddxPacket version='1.0'> <header/> <data> <struct> <var name='a'><string>string data</string></var> <var name='b'><number>123</number></var> <var name='c'><string>rye</string></var> <var name='d'><string>pastrami</string></var> </struct> </data> </wddxPacket> Variables are wrapped inside <var> tags with the variable name assigned as the value for the name attribute. Inside there is another set of tags that indicate the variable type: string, number, dateTime, boolean, array, binary, or recordSet. Finally, you have the data itself. You can also serialize one variable at a time using wddx_serialize_value : // one variable $s = wddx_serialize_value('Serialized', 'An optional comment'); This results in the following XML: <wddxPacket version='1.0'> <header> <comment>An optional comment</comment> </header> <data> <string>Serialized</string> </data> </wddxPacket> 12.11.4. See AlsoDocumentation on WDDX at http://www.php.net/wddx; more information at http://www.openwddx.org; Chapter 20, "Sharing Data with WDDX," from Programming ColdFusion, by Rob Brooks-Bilson (O'Reilly). Copyright © 2003 O'Reilly & Associates. All rights reserved. |
|