Using WDDX with PHP


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.

Encoding Data using WDDX

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.

Using the wddx_serialize_value() Function

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:

Listing 7-1: Serializing a Variable Using the wddx_serialize_value() Function
start example
 <?php $country="Australia"; $va=wddx_serialize_value($country); $fp=fopen("/var/www/html/example1.xml", "w+"); fwrite($fp, $va, strlen($va)); ?> 
end example
 

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:

click to expand: this figure shows the wddx packet that contains a string value, australia.
Figure 7-2: Using the wddx_serialize_value() Function to Generate 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:

Listing 7-2: Adding a Comment to a the WDDX Packet
start example
 <?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)); ?> 
end example
 

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:

click to expand: this figure shows the wddx packet that contains a comment string in the header element of the wddx packet.
Figure 7-3: WDDX Packet Containing a Comment

You can serialize an array of values using the wddx_serialize_value() function, as shown in Listing 7-3:

Listing 7-3: Serializing an Array Using the wddx_serialize_value() Function
start example
 <?php $countries=array("Australia", "India", "Kenya"); $va=wddx_serialize_value($countries); $fp=fopen("/var/www/html/example3.xml", "w+"); fwrite($fp, $va, strlen($va)); ?> 
end example
 

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:

click to expand: this figure shows the wddx packet that serializes the $countries variable containing an array of three elements.
Figure 7-4: WDDX Packet Representing an Array of Values

Using the wddx_serialize_vars() Function

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:

Listing 7-4: Serializing a Variable Using the wddx_serialize_vars() Function
start example
 <?php $country="Australia"; $va=wddx_serialize_vars("country"); $fp=fopen("/var/www/html/example4.xml", "w+"); fwrite($fp, $va, strlen($va)); ?> 
end example
 

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:

click to expand: this figure shows the wddx packet that contains a single variable, country, encoded.
Figure 7-5: WDDX Packet Representing a Single Encoded Variable
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:

Listing 7-5: Serializing Multiple Values Using the wddx_serialize_vars() Function
start example
 <?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)); ?> 
end example
 

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:

click to expand: this figure shows the wddx packet that serializes two variables, x and y.
Figure 7-6: WDDX Packet that Serializes Two Variables

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:

Listing 7-6: Using the wddx_serialize_vars() Function
start example
 <?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)); ?> 
end example
 

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:

click to expand: this figure shows the wddx packet that serializes an array of values.
Figure 7-7: WDDX Packet that Uses the wddx_serialize_vars() Function

Using the wddx_add_vars() Function

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:

  1. Create an empty WDDX packet that can hold the data. You can create a WDDX packet using the wddx_packet_start() function.

  2. Add data to the WDDX packet. You use the wddx_add_vars() function to add each variable to the WDDX packet.

  3. 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:

Listing 7-7: Creating a WDDX Packet Using the wddx_add_vars() Function
start example
 <?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)); ?> 
end example
 

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:

click to expand: this figure shows the wddx packet that contains the $country variable.
Figure 7-8: Adding Variables 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:

Listing 7-8: Using the wddx_add_vars() Function to Add More than One Variable
start example
 <?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)); ?> 
end example
 

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:

click to expand: this figure shows the <struct> element that contains three variables, name, department, and age, within the wddx structure.
Figure 7-9: Adding Multiple Variables Using the wddx_add_vars() Function
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 Data using WDDX

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:

Listing 7-9: Deserializing a Single PHP Variable
start example
 <?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); ?> 
end example
 

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:

click to expand: this figure shows the decoded value of the variable, $country.
Figure 7-10: Output of the wddx_deserialize() Function

The wddx_deserialize() function decodes a variable containing an array of values, as shown in Listing 7-10:

Listing 7-10: Deserializing an Array of Values
start example
 <?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); ?> 
end example
 

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:

click to expand: this figure shows the output of a wddx packet in the form of an associative array. after the wddx_deserialize() function deserializes a wddx packet, the data in the wddx packet is converted to the php variables.
Figure 7-11: Output of a WDDX Packet



Integrating PHP and XML 2004
Integrating PHP and XML 2004
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 51

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net