未捕获的异常“异常”带有消息“‘SimpleXMLElement’的序列化”是不允许的

发布于 2024-11-08 14:00:01 字数 672 浏览 3 评论 0原文

我不知道为什么会出现这种情况。我没有序列化 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

渔村楼浪 2024-11-15 14:00:01

您必须将 XML 数据转换为字符串,因为它们在内部都是 SimpleXMLElement

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

或者也许更优雅一点:

$game_data  = array();
$properties = array('sysreqos', 'sysreqmhz', 'sysreqmem', 'sysreqdx', 'sysreqhd');
foreach ($properties as $p) {
    $game_data[$p] = (string)$game->systemreq->pc->$p;
}

You have to cast the XML data to a string because internally they are all SimpleXMLElements.

$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;
}
君勿笑 2024-11-15 14:00:01

在类和对象文档中,有这样的内容:为了能够 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] 中抛出

会话的全部内容将丢失。希望这可以节省一些人的时间!

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

作者:Ray.Paseur

参考:http://php.net/manual/en/function。 unserialize.php

我所做的就是像“Stefan Gehrig”所说的那样,将 XML 数据转换为字符串

$_SESSION['obj'] = (string)$obj;

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!

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

what i do is as 'Stefan Gehrig' said, cast the XML data to a string

$_SESSION['obj'] = (string)$obj;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文