PHP consists of WDDX modules that contain serialization and deserialization functions to convert PHP variables into WDDX compatible data structures, and then reconvert them into the PHP format. PHP generates WDDX packets as a single string. For example, you can encode a PHP associative array in WDDX and transfer the array to a Perl script. The Perl script decodes the data in the WDDX packet and uses the data for further processing.
The WDDX API of PHP provides five functions to encode PHP to WDDX packet. These encoding functions are wddx_serialize_value(), wddx_serialize_vars(), wddx_add_vars(), wddx_packet_start(), and wddx_packet_end(). The WDDX API for PHP also provides a function to decode data from a WDDX packet.
The WDDX technology enables data exchange between the Web applications that are created in different programming languages and run on different platforms. The PHP functions that encode data into a WDDX packet are:
wddx_serialize_value() : Encodes a single variable into a WDDX packet.
wddx_serialize_vars() : Encodes more than one variable into a WDDX packet.
wddx_add_vars() : Adds PHP variables to a WDDX packet.
wddx_packet_start() : Creates a new WDDX packet.
wddx_packet_end() : Closes a WDDX packet.
The wddx_serialize_value() function creates a WDDX packet that contains a single value. The syntax of the wddx_serialize_value() function is:
string wddx_serialize_value (mixed var [, string comment])
In the above syntax:
var denotes the name of the variable that you need to add to a WDDX packet.
The wddx_serialize_value() function either returns a string that contains a WDDX packet, or the value, FALSE, if an error occurs. The wddx_serialize_value() function accepts variables as its first argument and converts the variables to a WDDX packet. The second argument of the wddx_serialize_value() function is optional and adds a comment to a WDDX packet.
Listing 7-1 shows how to use the wddx_serialize_value() function:
<?php $country="Australia"; $va=wddx_serialize_value($country); $fp=fopen("/var/www/html/example1.xml", "w+"); fwrite($fp, $va, strlen($va)); ?>
The above listing shows how to encode the $country variable to a WDDX packet using the wddx_serialize_value() function. The Web browser generates a parsed output and does not display the WDDX tags that are generated during the serialization process. The fwrite() function in the above listing writes the contents of the WDDX packet to the example.xml file.
Run the XML file to display the contents of the WDDX packet.
Figure 7-2 shows how the wddx_serialize_value() function generates a WDDX packet:
You can use the second parameter of the wddx_serialize_value() function to add a comment to the header of the WDDX packet, as shown in Listing 7-2:
<?php $country="Australia"; $va=wddx_serialize_value($country, "An example of WDDX"); $fp=fopen("/var/www/html/example2.xml", "w+"); fwrite($fp, $va, strlen($va)); ?>
The above listing shows the WDDX packet that contains the comment, An example of WDDX.
Figure 7-3 shows a WDDX packet that contains a comment:
You can serialize an array of values using the wddx_serialize_value() function, as shown in Listing 7-3:
<?php $countries=array("Australia", "India", "Kenya"); $va=wddx_serialize_value($countries); $fp=fopen("/var/www/html/example3.xml", "w+"); fwrite($fp, $va, strlen($va)); ?>
The above listing shows how to serialize three variables, India, Kenya, and Australia, using the wddx_serialize_value() function.
Figure 7-4 shows the WDDX packet representing an array of values:
The wddx_serialize_vars() function creates a WDDX packet with a serialized representation of the passed variables. The syntax of the wddx_serialize_vars() function is:
string wddx_serialize_vars (mixed var [, mixed..])
The wddx_serialize_vars() function accepts one or more variables as its arguments, which can either be strings or arrays containing strings.
Listing 7-4 shows how to serialize a single variable using the wddx_serialize_vars() function:
<?php $country="Australia"; $va=wddx_serialize_vars("country"); $fp=fopen("/var/www/html/example4.xml", "w+"); fwrite($fp, $va, strlen($va)); ?>
The above listing uses the wddx_serialize_vars() function to serialize a single variable into a WDDX packet.
Figure 7-5 shows the WDDX packet that serializes a variable using the wddx_serialize_vars() function:
Note | The serialization of a single variable using the wddx_serialize_vars() function generates a WDDX packet, which is different from the WDDX packet that the wddx_serialize_value() function generates. |
Listing 7-5 shows how to use the wddx_serialize_vars() function:
<?php $x="This is an example of the wddx_serialize_vars function."; $y=array("emp_name"=>"Kenny", "emp_id"=>"E01", "emp_dept"=>"Sales"); $va=wddx_serialize_vars("x", "y"); $fp=fopen("/var/www/html/example5.xml", "w+"); fwrite($fp, $va, strlen($va)); ?>
The above listing shows how to serialize two variables, x and y, using the wddx_serialize_vars() function.
Figure 7-6 shows the WDDX packet that serializes the two variables, x and y:
You can serialize more than two variables using the wddx_serialize_vars() function, with each variable containing an array of values, as shown in Listing 7-6:
<?php $student_names=array("Peter", "Crisp", "Ron"); $colleges=array("AEC", "BMBS"); $branches=array("IT", "EC", "EE"); $va=wddx_serialize_vars("student_names", "colleges", "branches"); $fp=fopen("/var/www/html/example6.xml", "w+"); fwrite($fp, $va, strlen($va)); ?>
The above listing shows how to use the wddx_serialize_vars() function to serialize three variables, $student_names, $branches, and $colleges, each containing an array of values.
Figure 7-7 shows the WDDX packet that serializes three variables: $student_names, $branches, and $colleges:
The wddx_add_vars() function adds one or more variables to a WDDX packet, which is represented by a packet ID. The syntax for the wddx_add_vars() function is:
boolean wddx_add_vars(int packet_id, mixed var1 [,mixed var2,...])
The wddx_add_vars() function returns either TRUE or FALSE. You should use two additional functions to generate a WDDX packet that uses the wddx_add_vars() function, which are wddx_packet_start() and wddx_packet_end().
The wddx_packet_start() function creates a new WDDX packet with the WDDX structure inside the packet. The syntax of the wddx_packet_start() function is:
int wddx_packet_start([string comment])
In the above syntax, the int keyword indicates that the return type of the wddx_packet_start() function is an integer.
The wddx_packet_start() function accepts an optional comment string as an argument and returns a packet ID. This function automatically creates a structure definition for including variables inside a WDDX packet.
The wddx_packet_end() function closes a WDDX packet specified by the packet ID and returns a string that contains the WDDX packet. The syntax of the wddx_packet_end() function is:
string wddx_packet_end(int packet_id)
In the above syntax, the string keyword indicates that the return type of the wddx_packet_end() function is a string. The argument that you pass to the wddx_packet_end() function identifies the WDDX packet that your application should decode.
To create a WDDX packet and add variables to the packet using the wddx_add_vars() function:
Create an empty WDDX packet that can hold the data. You can create a WDDX packet using the wddx_packet_start() function.
Add data to the WDDX packet. You use the wddx_add_vars() function to add each variable to the WDDX packet.
Close the WDDX packet using the wddx_packet_end() function.
The first argument in the wddx_add_vars() function specifies the WDDX packet ID to which you need to add data.
Listing 7-7 shows how to use the wddx_add_vars() function to create a WDDX packet:
<?php $country="Australia"; $id=wddx_packet_start("PHP"); wddx_add_vars($id, "country"); // contents of the packet are transferred to example7.xml file. $packet=wddx_packet_end($id); $fp=fopen("/var/www/html/example7.xml", "w+"); fwrite($fp, $packet, strlen($packet)); ?>
The above listing shows how to create a WDDX packet using the wddx_packet_start() function. You can add variables to a WDDX packet using the wddx_add_vars() function.
Figure 7-8 shows the variables that you add to a WDDX packet using the wddx_add_vars() function:
You can add more than one variable using the wddx_add_vars() function, as shown in Listing 7-8:
<?php $name="Ronald"; $department="Administration"; $Age="35"; $id=wddx_packet_start("PHP example"); wddx_add_vars($id, "name", "department", "Age"); $packet=wddx_packet_end($id); $fp=fopen("/var/www/html/example8.xml", "w+"); fwrite($fp, $packet, strlen($packet)); ?>
The above listing shows a WDDX packet that serializes three variables, name, department, and age.
The wddx_serialize_vars() and wddx_add_vars() functions use the <struct> element to store variables and their values. The wddx_serialize_value() function uses the <struct> element to store variables that represent associative arrays.
Figure 7-9 shows the WDDX packet that serializes three variables:
Note | The wddx_serialize_value() and wddx_serialize_vars() functions automatically create a WDDX packet, but you should provide a starting and a closing header for the wddx_add_vars() function. |
Decoding involves parsing a WDDX packet into an appropriate PHP variable. PHP contains a single function, wddx_deserialize(), to retrieve data from a WDDX packet. The wddx_deserialize() function decodes a WDDX packet. The syntax of the wddx_deserialize() function is:
mixed wddx_deserialize(string packet);
The above syntax shows that the wddx_deserialize() function accepts a single string argument that represents a WDDX packet. The wddx_deserialize() function returns a number, a string, or an array.
Listing 7-9 shows the deserialization of a PHP variable from a WDDX packet:
<?php $country="Australia"; //Serialize the variable using the wddx_serialize_value() function. $id=wddx_serialize_value($country); //Write the contents of the WDDX packet into an XML file. $fp=fopen("/var/www/html/example9.xml", "w+"); fwrite($fp, $id, strlen($id)); //Deserialize the WDDX packet. $output=wddx_deserialize($id); //Display the decoded value of the PHP variable. print($output); ?>
The above listing shows how to retrieve data from a WDDX packet using the wddx_deserialize() function.
Figure 7-10 shows deserialized data of the WDDX packet in Listing 7-9:
The wddx_deserialize() function decodes a variable containing an array of values, as shown in Listing 7-10:
<?php $names=array("John", "Christine", "Joseph"); //Serializing an array of values. $id=wddx_serialize_value($names); //Deserializing the WDDX packet identified by $id. $output=wddx_deserialize($id); print_r($output); ?>
The above listing shows how to deserialize a WDDX packet that contains an array of values.
Figure 7-11 shows the output of Listing 7-10: