PHP: WDDX 関数 - Manual
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: Tue, 21 Dec 2004

CXXV. WDDX 関数

導入

以下の関数は、 と組み合わせて 動作することを想定しています。

要件

WDDX を使用するには、(Apache 1.3.7以降に付属する)expatライブラリを インストールする必要があります。

インストール手順

expatをインストールした後、 --enable-wddxを指定してPHPを コンパイルする必要があります。

Windows版のPHPには この拡張モジュールのサポートが組み込まれています。これらの関数を使用 するために拡張モジュールを追加でロードする必要はありません。

実行用の設定

この拡張モジュールは設定ディレクティブを全く定義しません。

リソース型

この拡張モジュールはリソース型を全く定義しません。

定義済みの定数

この拡張モジュールは定数を全く定義しません。

変数をシリアル化する全ての関数は配列の最初要素をその配列が配列と 構造体のどちらでシリアル化されるのかを定義するために使用するとい うことに注意して下さい。最初の要素が文字列をキーとして有する場合 は構造体でシリアル化され、その他の場合は配列でシリアル化されます。

例 1. 単一の値のシリアル化

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

この例は次の出力を行います。

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

例 2. パケットを追加する例

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

この例は次のような出力を行います。

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

注意: ASCII以外の文字をシリアル化したい場合、これを行う前に 適切にロケールを設定する必要があります。 (setlocale()を参照して下さい)

目次
wddx_add_vars -- 指定したIDのWDDXパケットを追加する
wddx_deserialize -- シリアル化された WDDX パケットを元に戻す
wddx_packet_end -- 指定した ID の WDDX パケットを終了する
wddx_packet_start --  新規の WDDX パケットを内部の構造体を用いて開始する
wddx_serialize_value -- 値をWDDXパケットにシリアル化する
wddx_serialize_vars -- 変数を WDDX パケットにシリアル化する


add a note add a note User Contributed Notes
WDDX 関数
Jimmy Wimenta
16-Jul-2004 12:53
PHP's WDDX is useful only for exchanging data between PHP applications, but definetly not for exchanging data between different languages (which actually defeats the purpose of WDDX).

For example:

$hash1 = array ("2" => "Two", "4" => "Four", "5" => "Five");
$hash2 = array ("0" => "Zero", "1" => "One", "2" => "Two");

$hash1 will be serialized as hash, but
$hash2 will be serialized as array/list, because the key happen to be a sequence starting from 0.

Unless the library provide a way for users to specify the type, it can never be used for cross-platform data exchange.
Q1tum at hotmail dot com
21-Nov-2003 01:08
To insert arrays into a wddx variable here is a fine way to do it:

<?php

$sql
= 'SELECT * FROM example';
$query = mysql_query($sql, $db) or die(mysql_error());

while(
$result = mysql_fetch_array($query)) {
  
$id[] = $result[ 'id'];
  
$name[] = $result['name'];
  
$description[] = $result[$prefix . 'description'];
}

mysql_free_result($query);

wddx_add_vars($packet_id, "id");
wddx_add_vars($packet_id, "name");
wddx_add_vars($packet_id, "description");

$wddxSerializeValue = wddx_packet_end($packet_id);

?>
pierre dot jeunejean at adnexus dot be
26-Sep-2003 12:11
This is, to me, a nice way to solve the date problem.
In this example, we use a custom Date Object to store a date.
We show the serialized value in a textarea. We unserialize the packet and then we loop trough the returned array to get each element value and the data type of each of those values.

Here is the code, and after it its ouptut:

<?PHP
class Date {
   var
$date = null;
  
   function
Date ($date) {
      
$this->date = $date;
   }
}

$textValue = "Per idem tempus annorum.";
$numberValue = 23;
$booleanValue = true;
$null = null;
$dateValue = new Date('22/03/2003');

$packet_id = wddx_packet_start();

wddx_add_vars($packet_id, "dateValue");
wddx_add_vars($packet_id, "textValue");
wddx_add_vars($packet_id, "numberValue");
wddx_add_vars($packet_id, "booleanValue");
wddx_add_vars($packet_id, "null");

$wddxSerializeValue = wddx_packet_end($packet_id);

echo(
"<FORM><TEXTAREA rows=15 cols=80>" .
      
$wddxSerializeValue .
  
"</TEXTAREA></FORM>");
  
$wddxDeserialize = wddx_deserialize($wddxSerializeValue);

foreach (
$wddxDeserialize as $key => $value) {
   if(
is_object($value)) { //checks if the value is an object
      
$valueType = get_class($value); //gets the NAME of the class, so the type of the current value
      
$value = $value->date; //gets the value of the date property of the current Date Object
  
} else {
      
$valueType = gettype($value);
   }
   echo(
"Value = " . $value . "&nbsp;&nbsp;&nbsp;(Value type = " . $valueType . ")<br>");
}
?>

<wddxPacket version='1.0'>
<header/>
<data><struct>
<var name='dateValue'><struct>
<var name='php_class_name'><string>date</string></var>
<var name='date'><string>22/03/2003</string></var>
</struct></var>
<var name='textValue'><string>Per idem tempus annorum.</string></var>
<var name='numberValue'><number>23</number></var>
<var name='booleanValue'><boolean value='true'/></var>
<var name='null'><null/></var>
</struct></data>
</wddxPacket>

Value = 22/03/2003  (Value type = date)
Value = Per idem tempus annorum.  (Value type = string)
Value = 23  (Value type = integer)
Value = 1  (Value type = boolean)
Value =    (Value type = NULL)
12-Sep-2003 02:29
wddx isn't 100% perl compatible .. I have an wddx file infront of me and it only works with php so better don't use it
pointsystems.com, sbarnum
04-Sep-2002 05:11
a good FAQ on WDDX can be found here:
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.
philip at thepr()jects dot ()rg
17-Nov-2000 09:32
Tutorial here :

XML and PHP. Part 1: Using the WDDX functions
djm at web dot us dot uu dot net
02-Mar-2000 11: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:<br>" . $value->{"pi"} . "<p>\n";
print "cities is:<br>\n";
$key = 0;
foreach $val (@{$value->{"cities"}}) {
   print "$key => $val<br>\n";
   $key++;
}
</PRE>
djm at web dot us dot uu dot net
02-Mar-2000 11: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>
<P>
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:<br>" . $value->{"pi"} . "<p>\n";
print "cities is:<br>\n";
$key = 0;
foreach $val (@{$value->{"cities"}}) {
   print "$key => $val<br>\n";
   $key++;
}
</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:
<br>
<a href="">
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:<br>" . $value["pi"] . "<p>\n";
print "cities is:<br>\n";
while (list($key, $val) = each($value["cities"])) {
   print "$key => $val<br>\n";
}
</pre>

which outputs:

<pre>
pi is:
3.1415926

cities is:
0 => Austin
1 => Novato
2 => Seattle
</pre>
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:

$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);

<w32api_set_call_methodwddx_add_vars>
 Last updated: Tue, 21 Dec 2004
show source | credits | sitemap | contact | advertising | mirror sites 
Copyright © 2001-2005 The PHP Group
All rights reserved.
This unofficial mirror is operated at: /
Last updated: Mon Mar 14 08:13:06 2005 Local time zone must be set--see zic manual page