[ Team LiB ] Previous Section Next Section

20.3 WDDX

The Web Distributed Data Exchange, or WDDX, is an XML language for describing data in a way that facilitates moving it from one programming environment to another. The intent is to relieve difficulty associated with sending data between applications that represent data differently. Traditionally this has been done by designing special interfaces for each case. For instance, you may decide that your PERL script will write out its three return data separated with tabs, using a regular expression to extract the text you later convert to integers. WDDX intends to unify the effort into a single interface. If you wish to learn more about WDDX, visit the home site at <http://www.openwddx.org/>.

Andrei Zmievski added WDDX support to PHP.

wddx_add_vars(integer packet_identifier, string variable, …)

The wddx_add_vars function is one of three functions for creating packets incrementally. After creating a packet with wddx_packet_start, you may add as many variables as you wish with wddx_add_vars. After the packet_identifier argument, you may pass strings with the names of variables in the local scope or arrays of strings. If necessary, PHP will explore multidimensional arrays for names of variables. The variables will be added to the packet until you use wddx_packet_end to create the actual packet as a string.

value wddx_deserialize(string packet)

The wddx_deserialize function (Listing 20.14) returns a variable representing the data contained in a WDDX packet. If the packet contains a single value, it will be returned as an appropriate type. If the packet contains multiple values in a structure, an associative array will be returned.

Listing 20.14 wddx_deserialize
<?php
    //simulate WDDX packet
    $packet = "<wddxPacket version='1.0'>" .
        "<data>" .
        "<string>Core PHP Programming</string>" .
        "</data>" .
        "</wddxPacket>";

    //pull data out of packet
    $data = wddx_deserialize($packet);

    //test the type of the variable
    if(is_array($data))
    {
        //loop over each value
        foreach($data as $key=>$value)
        {
            print("$key: $value<br>\n");
        }
    }
    else
    {
        //simply print the value
        print("$data<br>\n");
    }
?>

string wddx_packet_end(integer packet_identifier)

The wddx_packet_end function returns a string for the packet created with wddx_packet_start and wddx_add_vars.

integer wddx_packet_start(string comment)

The wddx_packet_start function (Listing 20.15) returns an identifier to a WDDX packet you can build as you go. The optional comment argument will be placed in the packet if supplied. Use the returned packet identifier with wddx_add_vars and wddx_packet_end.

Listing 20.15 wddx_packet_start
<?php
    //create test data
    $Name = "Leon Atkinson";
    $Email = "corephp@leonatkinson.com";
    $Residence = "Martinez";

    $Info = array("Email", "Residence");

    //start packet
    $wddx = wddx_packet_start("Core PHP Programming");

    //add some variables to the packet
    wddx_add_vars($wddx, "Name", $Info);

    //create packet
    $packet = wddx_packet_end($wddx);

    //print packet for demonstration purposes
    print($packet);
?>

string wddx_serialize_value(value data, string comment)

The wddx_serialize_value function creates a WDDX packet containing a single value. The data will be encoded with no name. The optional comment field will be added to the packet as well.

string wddx_serialize_vars(string variable, …)

Use wddx_serialize_vars (Listing 20.16) to create a packet containing many variables. You may specify any number of variable names in the local scope. Each argument may be a string or an array. PHP will recursively explore multidimensional arrays for more names of variables if necessary. A WDDX packet is returned.

Listing 20.16 wddx_serialize_vars
<?php
    //create test data
    $Name = "Leon Atkinson";
    $Email = "corephp@leonatkinson.com";
    $Residence = "Martinez";

    $Info = array("Email", "Residence");

    //print packet
    print(wddx_serialize_vars("Name", $Info));
?>
    [ Team LiB ] Previous Section Next Section