©
本文檔使用 php中文網手册 發布
(PHP 4, PHP 5)
wddx_deserialize — Unserializes a WDDX packet
$packet
)
Unserializes a WDDX packet
.
packet
A WDDX packet, as a string or stream.
Returns the deserialized value which can be a string, a number or an array. Note that structures are deserialized into associative arrays.
Do not pass untrusted user input to wddx_deserialize() . Unserialization can result in code being loaded and executed due to object instantiation and autoloading, and a malicious user may be able to exploit this.
[#1] valer underscore crisan at yahoo [2009-04-01 14:03:09]
In PHP 5.2.9, we noticed that wddx serialize and deserialize seems to use UTF-8 encoding for XML input and the output data. There are some posts below that talk about fixing the input XML data so deserialize would work fine on special chars in ISO-8859-1 encoding, but none of the posts talks about the output encoding.
Consider this test:
$text = "J??r";
$packet = wddx_serialize_value($text);
$header = '
<?phpxml version="1.0" encoding="iso-8859-1"?>
';
$newText = wddx_deserialize($header . $packet);
echo "WDDX Packet: $packet\n";
echo "Deserialized Object: $newText\n";
The output is:
WDDX Packet: <wddxPacket version='1.0'><header/><data><string>J??r</string></data></wddxPacket>
Deserialized Object: J??r
The deserialized string is not in ISO-8859-1 encoding. It is in UTF-8 encoding. So we need to run utf8_decode() on the deserialized data to convert it back to ISO-8859-1 encoding which is what our application expects.
Hope this helps.
[#2] Magnus Deininger, dma05 at web dot de [2009-02-12 05:57:15]
When writing your wddx file manually with an UTF-8 aware editor and saving it in utf-8, if your data gets its special characters mysteriously scrambled, try to add an xml header that marks the output as iso-8859-1, like this one:
<?phpxml version="1.1" encoding="iso-8859-1" ?>
This makes the wddx decode function treat the input as iso-8859-1, so it will not try to treat it as utf-8 and do an implicit decode to iso-8859-1. You will then have read all string data in the wddx packet in their original utf-8 encoding, so that 'echo' and other output functions will produce the intended result if you have set the output encoding to utf-8.
(Bugs reports on this behaviour seem to be treated as bogus, so it would seem in order to point out this incorrect and highly confusing side-effect.)
[#3] dormilich at netscape dot net [2008-11-28 05:13:25]
When deserializing objects make sure you have the class definition loaded. wddx_deserialize() doesn't call the class itself, so you will receive a fatal error.
Nevertheless you can look for the class manually and delegate it to __autoload().
<?php
// $wddx_string needs to be valid XML to be loaded by SimpleXML.
// class_exists() will call the __autoload() function. if you don't
// want to use __autoloload(), use require_once()
function loadClassesFromWDDX($wddx_string)
{
$xml = new SimpleXMLElement($wddx_string);
foreach ($xml->xpath('//var') as $var)
{
if ($var['name'] == 'php_class_name')
{
if (!class_exists($var->string))
{
throw new Exception('Class '" . $var->string . "'not available.');
// trigger_error('Class '" . $var->string . "'not available.', E_USER_ERROR);
}
}
}
}
?>
[#4] dave at codexweb dot co dot za [2006-01-21 04:40:42]
Here's a handy wddx_deserialize clone I wrote a while back. It uses PHP5 SimpleXML and recursion to do pretty much the same task as wddx_deserialize. Hope it comes in handy for someone.
<?php
if (!function_exists('wddx_deserialize'))
{
function wddx_deserialize($xmlpacket)
{
if ($xmlpacket instanceof SimpleXMLElement)
{
if (!empty($xmlpacket->struct))
{
$struct = array();
foreach ($xmlpacket->xpath("struct/var") as $var)
{
if (!empty($var["name"]))
{
$key = (string) $var["name"];
$struct[$key] = wddx_deserialize($var);
}
}
return $struct;
}
else if (!empty($xmlpacket->array))
{
$array = array();
foreach ($xmlpacket->xpath("array
$nsname
)$from
, mixed $to
)$src
, mixed $diff
)