HomePHP Page 6 - Data Exchange with XML, WDDX and PHP
Hip To Be Square - PHP
You may never have heard of it before - but if your job involvesexchanging data between different servers and applications, you're going tobe hearing a lot about it very soon. Find out how WDDX makes it possible tocreate and transmit platform-neutral data structures across the Web, andhow it can be combined with PHP to create a whole new generation of Webapplications (including new content syndication and financial updateservices).
The wddx_packet_start(), wddx_add_vars() and wddx_packet_end() functions are used in tandem, to create a WDDX packet and add variables to it one after another. This comes in handy if you're adding values in a loop, or retrieving them from a database. Consider the following example, which creates a WDDX structure containing the squares of numbers between 1 and 10.
<?
// variable prefix
$prefix="square_of_";
// create packet
$wp = wddx_packet_start();
// add variables to it
for ($x=1; $x<=10; $x++)
{
// dynamically generate variable name
$varname = $prefix . $x;
$$varname = pow($x, 2);
// add to packet
wddx_add_vars($wp, "$prefix$x");
}
// end and print
echo wddx_packet_end($wp);
?>
In this case, I'm first starting a new WDDX packet with the wddx_packet_start() function, which returns a WDDX resource handle for further use; this handle is used for all subsequent operations.
Next, I generate the squares of all numbers between 1 and 10 using a loop, dynamically generate a variable name to hold the value, and add this variable to the packet via the wddx_add_vars() function. This function works exactly like wddx_serialize_vars() - you can specify a list of variables to be added if you like - and requires you to specify the WDDX resource handle generated in the first step.
Once the packet has been generated, the wddx_packet_end() function is used to add the closing tags to the generated WDDX packet.