php 未捕获的异常 'Exception' 带有消息 'SimpleXMLElement' 的序列化是不允许的'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6058966/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Uncaught exception 'Exception' with message 'Serialization of 'SimpleXMLElement' is not allowed'
提问by user761088
I am not sure why this is coming up. I am notserializing the XML, but my array that I created from an RSS feed (note this is just a snippet):
我不确定为什么会出现这种情况。我没有序列化 XML,而是我从 RSS 提要创建的数组(注意这只是一个片段):
$game_data = array (
'sysreqos' => $game->systemreq->pc->sysreqos,
'sysreqmhz' => $game->systemreq->pc->sysreqmhz,
'sysreqmem' => $game->systemreq->pc->sysreqmem,
'sysreqdx' => $game->systemreq->pc->sysreqdx,
'sysreqhd' => $game->systemreq->pc->sysreqhd,
);
Then I serialize it $some_var = serialize($game_data)
and write to a text file fputs($fh,$some_var)
.
然后我将它序列化$some_var = serialize($game_data)
并写入文本文件fputs($fh,$some_var)
。
But it does not get that far, it errors out on the serialize line:
但它并没有那么远,它在序列化行上出错:
Uncaught exception 'Exception' with message 'Serialization of 'SimpleXMLElement' is not allowed'
未捕获的异常 'Exception' 带有消息 'SimpleXMLElement' 的序列化是不允许的'
回答by Stefan Gehrig
You have to cast the XML data to a string because internally they are all SimpleXMLElement
s.
您必须将 XML 数据转换为字符串,因为在内部它们都是SimpleXMLElement
s。
$game_data = array (
'sysreqos' => (string)$game->systemreq->pc->sysreqos,
'sysreqmhz' => (string)$game->systemreq->pc->sysreqmhz,
'sysreqmem' => (string)$game->systemreq->pc->sysreqmem,
'sysreqdx' => (string)$game->systemreq->pc->sysreqdx,
'sysreqhd' => (string)$game->systemreq->pc->sysreqhd
);
Or perhaps a little bit more elegant:
或者也许更优雅一点:
$game_data = array();
$properties = array('sysreqos', 'sysreqmhz', 'sysreqmem', 'sysreqdx', 'sysreqhd');
foreach ($properties as $p) {
$game_data[$p] = (string)$game->systemreq->pc->$p;
}
回答by abdulwadood
In the Classes and Objects docs, there is this: In order to be able to unserialize() an object, the class of that object needs to be defined.
在类和对象文档中,有这样的:为了能够反序列化()一个对象,需要定义该对象的类。
Prior to PHP 5.3, this was not an issue. But after PHP 5.3 an object made by SimpleXML_Load_String() cannot be serialized. An attempt to do so will result in a run-time failure, throwing an exception. If you store such an object in $_SESSION, you will get a post-execution error that says this:
在 PHP 5.3 之前,这不是问题。但是在 PHP 5.3 之后,由 SimpleXML_Load_String() 创建的对象不能被序列化。尝试这样做将导致运行时失败,引发异常。如果您将这样的对象存储在 $_SESSION 中,您将收到一个执行后错误,内容如下:
Fatal error: Uncaught exception 'Exception' with message 'Serialization of 'SimpleXMLElement' is not allowed' in [no active file]:0 Stack trace: #0 {main} thrown in [no active file] on line 0
致命错误:在 [无活动文件]:0 中出现消息“不允许序列化 'SimpleXMLElement' 的未捕获异常'异常':0 堆栈跟踪:#0 {main} 在第 0 行的 [无活动文件] 中抛出
The entire contents of the session will be lost. Hope this saves someone some time!
会话的全部内容将丢失。希望这可以节省一些时间!
<?php // RAY_temp_ser.php
error_reporting(E_ALL);
session_start();
var_dump($_SESSION);
$_SESSION['hello'] = 'World';
var_dump($_SESSION);
// AN XML STRING FOR TEST DATA
$xml = '<?xml version="1.0"?>
<families>
<parent>
<child index="1" value="Category 1">Child One</child>
</parent>
</families>';
// MAKE AN OBJECT (GIVES SimpleXMLElement)
$obj = SimpleXML_Load_String($xml);
// STORE THE OBJECT IN THE SESSION
$_SESSION['obj'] = $obj;
By: Ray.Paseur
作者:雷·帕瑟
Ref: http://php.net/manual/en/function.unserialize.php
参考:http: //php.net/manual/en/function.unserialize.php
what i do is as 'Stefan Gehrig' said, cast the XML data to a string
我所做的是正如“Stefan Gehrig”所说,将 XML 数据转换为字符串
$_SESSION['obj'] = (string)$obj;