Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uncaught exception 'Exception' with message 'Serialization of 'SimpleXMLElement' is not allowed'

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'

like image 703
user761088 Avatar asked May 19 '11 12:05

user761088


2 Answers

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;
like image 28
abdulwadood Avatar answered Sep 20 '22 11:09

abdulwadood


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;
}
like image 107
Stefan Gehrig Avatar answered Sep 16 '22 11:09

Stefan Gehrig