未捕获的异常“异常”带有消息“‘SimpleXMLElement’的序列化”是不允许的
我不知道为什么会出现这种情况。我没有序列化 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,
);
然后我序列化它$some_var = serialize($game_data) 并写入文本文件
fputs($fh,$some_var)
。
但它并没有走到这一步,它在序列化行上出错了:
未捕获的异常“Exception”,消息为“不允许序列化“SimpleXMLElement””
I am not sure why this is coming up. I am not serializing the XML, but my array that I created from an RSS feed (note this is just a snippet):
$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)
.
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'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须将 XML 数据转换为字符串,因为它们在内部都是
SimpleXMLElement
。或者也许更优雅一点:
You have to cast the XML data to a string because internally they are all
SimpleXMLElement
s.Or perhaps a little bit more elegant:
在类和对象文档中,有这样的内容:为了能够 unserialize() 对象,需要定义该对象的类。
在 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堆栈跟踪:#0 {main} 在第 0 行 [no active file] 中抛出
会话的全部内容将丢失。希望这可以节省一些人的时间!
作者:Ray.Paseur
参考:http://php.net/manual/en/function。 unserialize.php
我所做的就是像“Stefan Gehrig”所说的那样,将 XML 数据转换为字符串
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:
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
The entire contents of the session will be lost. Hope this saves someone some time!
By: Ray.Paseur
Ref: http://php.net/manual/en/function.unserialize.php
what i do is as 'Stefan Gehrig' said, cast the XML data to a string