PHP  
downloads | documentation | faq | getting help | mailing lists | | php.net sites | links | my php.net 
search for in the  
<w32api_set_call_methodwddx_add_vars>
view the version of this page
Last updated: Sat, 19 Apr 2003

CVI. WDDX Funktionen

Diese Funktionen arbeiten mit zusammen.

Es ist zu beachten, dass alle Funktionen, die Variablen serialisieren, immer das erste Element eines Arrays benutzen, um festzustellen, ob das Array in ein Array oder eine 'Structure' serialisiert wird. Wenn das erste Element einen String als Schl�ssel hat, wird das Array in eine 'Structure' serialisiert, andernfalls in ein Array.

Beispiel 1. Einen einzelnen String serialisieren

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

dieses Beispiel erzeugt:

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

Beispiel 2. Die Verwendung von inkrementierenden Paketen

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

/* So tun, als ob $cities aus einer Datenbank kommt */
$cities = array("Austin", "Novato", "Seattle");
wddx_add_vars($packet_id, "cities");

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

Diese Beispiel wird erzeugen:

<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>

Inhaltsverzeichnis
wddx_add_vars --  F�gt dem WDDX Paket mit der �bergebenen ID Werte hinzu
wddx_deserialize -- Deserialisiert ein WDDX Paket
wddx_packet_end --  Schliesst das WDDX Paket mit der angegebenen ID
wddx_packet_start --  Beginnt ein neues WDDX Paket mit einer 'Structure'
wddx_serialize_value --  Serialisiert einen einzelnen Wert in ein WDDX Paket
wddx_serialize_vars -- Serialisiert Variablen in WDDX Pakete


User Contributed Notes
WDDX Funktionen
add a note
yzhang at sfu dot ca
26-May-1999 07: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>

djm at web dot us dot uu dot net
19-Oct-1999 03: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>

djm at web dot us dot uu dot net
20-Oct-1999 08: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=">>

djm at web dot us dot uu dot net
02-Mar-2000 10: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>

djm at web dot us dot uu dot net
02-Mar-2000 10: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>

eric at boca dot hollywood dot com
09-May-2000 10:36

If you take a wddx recordset (I.E. in Cold Fusion) will Deserializing the packet create a Result varible?
philip at thepr()jects dot ()rg
17-Nov-2000 08:32

Tutorial here :

XML and PHP. Part 1: Using the WDDX functions

bradburn at kiwi dot de
30-Jul-2002 02:02

With ref to the above comment about typing, I have found that -- oddly enough -- PHP's WDDX supports the following WDDX types: null, boolean (true/false), number and string, *but* not date-time.

as an example, use the following values in an array that you then serialize:

$number = 5,
$null = NULL,
$bool = true,
$string = 'this is a string'.

they will all serialize correctly, e.g. the third entry comes out as:

<var name='bool'><boolean value='true'/></var>

i have tried with the 'official' format for WDDX 'datetime', e.g. '1998-9-15T09:05:32+4:0' (from the DTD @ ) but have only succeeded in getting this encoded as a 'string' type.

if anyone else has any more information on this, it would be welcome. i would like to store the variables in 'appropriate' fields in a database, and the fact that only datetime is not supported is slightly irritating -- otherwise it would be a very useful function.

pointsystems.com, sbarnum
04-Sep-2002 05:11

a good FAQ on WDDX can be found here:

add a note

<w32api_set_call_methodwddx_add_vars>
 Last updated: Sat, 19 Apr 2003
show source | credits | mirror sites 
Copyright © 2001-2003 The PHP Group
All rights reserved.
This mirror generously provided by: /
Last updated: Thu May 15 01:08:38 2003 CEST