PHP  
downloads | documentation | faq | getting help | | php.net sites | links 
search for in the  
previousw32api_set_call_methodwddx_add_varsnext
Last updated: Tue, 28 May 2002
view this page in Printer friendly version | English | Brazilian Portuguese | Czech | Dutch | French | German | Hungarian | Italian | Japanese | Korean | Polish | Romanian | Russian | Spanish | Turkish

CII. WDDX Functions

These functions are intended for work with .

In order to use WDDX, you will need to install the expat library (which comes with apache 1.3.7 or higher) and recompile PHP with --with-xml and --enable-wddx.

Huomaa: If you want to serialize non-ASCII characters you have to set the appropriate locale before doing so (see setlocale()).

All the functions that serialize variables use the first element of an array to determine whether the array is to be serialized into an array or structure. If the first element has string key, then it is serialized into a structure, otherwise, into an array.

Esimerkki 1. Serializing a single value

<?php
print wddx_serialize_value("PHP to WDDX packet example", "PHP packet");
?>

This example will produce:

<wddxPacket version='1.0'><header comment='PHP packet'/><data>
<string>PHP to WDDX packet example</string></data></wddxPacket>

Esimerkki 2. Using incremental packets

<?php
$pi = 3.1415926;
$packet_id = wddx_packet_start("PHP");
wddx_add_vars($packet_id, "pi");

/* Suppose $cities came from database */
$cities = array("Austin", "Novato", "Seattle");
wddx_add_vars($packet_id, "cities");

$packet = wddx_packet_end($packet_id);
print $packet;
?>

This example will produce:

<wddxPacket version='1.0'><header comment='PHP'/><data><struct>
<var name='pi'><number>3.1415926</number></var><var name='cities'>
<array length='3'><string>Austin</string><string>Novato</string>
<string>Seattle</string></array></var></struct></data></wddxPacket>

Sis�llys
wddx_add_vars --  Add variables to a WDDX packet with the specified ID
wddx_deserialize -- Deserializes a WDDX packet
wddx_packet_end -- Ends a WDDX packet with the specified ID
wddx_packet_start --  Starts a new WDDX packet with structure inside it
wddx_serialize_value -- Serialize a single value into a WDDX packet
wddx_serialize_vars -- Serialize variables into a WDDX packet
User Contributed Notes
WDDX Functions
add a note about notes

26-May-1999 06:29

To make these examples work, you'll probably want to format the output with
a call to htmlentities:

<pre>
$pi = 3.1415926;
$packet_id = wddx_packet_start("PHP");
wddx_add_vars($packet_id, "pi");

/* Suppose $cities came from database */
$cities = array("Austin", "Novato",
"Seattle");
wddx_add_vars($packet_id, "cities");

$packet = wddx_packet_end($packet_id);
print htmlentities($packet);
</pre>


19-Oct-1999 02:45

Since there aren't any examples of reversing the process, here's one. If
you had the packet produced by the above example (without the
htmlentities() call), you could retrieve the values like this:

<pre>
$value = wddx_deserialize($packet);
print "pi is:
" . $value["pi"] . "

\n"; print "cities is:
\n"; while (list($key, $val) = each($value["cities"])) { print "$key => $val
\n"; } </pre> which outputs: <pre> pi is: 3.1415926 cities is: 0 => Austin 1 => Novato 2 => Seattle </pre>



20-Oct-1999 07:30

The PHP WDDX module encodes all scalars as strings in the XML packet, due
to PHP's loose typing.  The current Perl WDDX module takes a different
approach, requiring the caller to mark each variable with a type.  This is
helpful if the reader of the packet is a more strongly typed language that
distinguishes between string, int, float, and bool; but it's more
cumbersome to use than the PHP approach.  The Perl module is hard to find
right now; its home page is:

<a href=">>


02-Mar-2000 09:36

I think it would be helpful for passing data between languages to show a
direct translation of the above examples into Perl, using WDDX.pm 1.00
from CPAN.  It took me awhile to figure out.  To serialize:
<PRE>
#!/usr/bin/perl

use WDDX;

$wddx = new WDDX;
$packet_id = $wddx->struct({});

$pi = 3.1415926;
$packet_id->set("pi" => $wddx->number($pi));

# Suppose @cities came from database
@cities = ("Austin", "Novato", "Seattle");
$packet_id->set("cities" => $wddx->array([map
$wddx->string($_), @cities]));

$packet = $wddx->serialize($packet_id);

open(FP, ">cities.wddx");
print FP $packet;
close(FP);
</PRE>

To deserialize: <PRE> #!/usr/bin/perl use WDDX; open(FP, "<cities.wddx"); undef $/; # Slurp the whole file. $packet = <FP>; close(FP); $packet_id = new WDDX; $wddx_obj = $packet_id->deserialize($packet); $value = $wddx_obj->as_hashref(); print "pi is:
" . $value->{"pi"} . "

\n"; print "cities is:
\n"; $key = 0; foreach $val (@{$value->{"cities"}}) { print "$key => $val
\n"; $key++; } </PRE>



02-Mar-2000 09:50

Here's a rewrite of the deserializing Perl code that uses variable names
consistently with the serializing example.  Sorry for any confusion....
<PRE>
#!/usr/bin/perl

use WDDX;

open(FP, "<cities.wddx");
undef $/;                       # Slurp the whole file.
$packet = <FP>;
close(FP);

$wddx = new WDDX;
$packet_id = $wddx->deserialize($packet);
$value = $packet_id->as_hashref();

print "pi is:
" . $value->{"pi"} . "

\n"; print "cities is:
\n"; $key = 0; foreach $val (@{$value->{"cities"}}) { print "$key => $val
\n"; $key++; } </PRE>



09-May-2000 09:36

If you take a wddx recordset (I.E. in Cold Fusion) will Deserializing the
packet create a Result varible?


17-Nov-2000 07:32

Tutorial here :

XML and PHP. Part 1: Using the WDDX functions

add a note about notes
previousw32api_set_call_methodwddx_add_varsnext
Last updated: Tue, 28 May 2002
show source | credits | stats | mirror sites:  
Copyright © 2001, 2002 The PHP Group
All rights reserved.
This mirror generously provided by:
Last updated: Sat Jul 6 00:05:55 2002 CEST