Throw exception "Exception" with the message "Serialization" SimpleXMLElement "not allowed"

I do not know why this is happening. I do not serialize XML, but my array that I created from the RSS feed (note that 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 to $some_var = serialize($game_data) and write it to the text file fputs($fh,$some_var) .

But this is not so far, this is an error in the serialization line:

Unable exception "Exception" with the message "Serialization" SimpleXMLElement "not allowed"

+7
source share
2 answers

You need to pass the XML data to a string, because inside they are all 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 maybe a little more elegant:

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

The documents of classes and objects have the following: In order to be able to unserialize () an object, the class of this object must be defined.

Prior to PHP 5.3, this was not a problem. But after PHP 5.3, the object created by SimpleXML_Load_String () cannot be serialized. Attempting to do this will result in a run-time failure, throwing an exception. If you store such an object in $ _SESSION, you will receive an error message after this:

Fatal error: throw exception "Exception" with the message "Serialization" SimpleXMLElement "not allowed" in [no active file]: 0 Trace of the stack: # 0 {main} selected [no active file] in line 0

All session content will be lost. Hope this helps someone!

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

Posted by: Ray.Paseur

Link: http://php.net/manual/en/function.unserialize.php

what am I doing since "Stefan Gerig" said passed the XML data to a string

 $_SESSION['obj'] = (string)$obj; 
+3
source

All Articles