5

Hi all I have a site developed in codeigniter. I'm parsing an xml that I retrieve from a server and I want to put the return value into a session variable. But return me this error:

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

My PHP version on my vps is: PHP Version 5.3.10-1ubuntu3.4

This is my code:

$xml = new SimpleXMLElement(curl_exec($ch2));
$error2=curl_getinfo( $ch2, CURLINFO_HTTP_CODE );
curl_close($ch2);
foreach ($xml->DATA as $entry){
    $code_travco = $entry->attributes()->COUNTRY_CODE;
    $name_en =  $entry->COUNTRY_NAME;
    $newdata = array(
        'code'  => $code_travco,
        'name_en'     =>  $name_en
    );
    $this->session->set_userdata($code_travco.'_nation_en', $newdata);      
 } 
Alessandro Minoccheri
  • 35,521
  • 22
  • 122
  • 171

1 Answers1

14

may be try changing it to string before adding, like:

foreach ($xml->DATA as $entry){
    $code_travco = (string) $entry->attributes()->COUNTRY_CODE;
    $name_en =  (string) $entry->COUNTRY_NAME;
    $newdata = array(
        'code'  => $code_travco,
        'name_en'     =>  $name_en
    );
    $this->session->set_userdata($code_travco.'_nation_en', $newdata);      
 } 
Sudhir Bastakoti
  • 99,167
  • 15
  • 158
  • 162
  • it works great! But why isn't a string? which format is before put (string)? – Alessandro Minoccheri Mar 18 '13 at 09:57
  • @AlessandroMinoccheri its resource type and resources cannot be serialized, hence the error.. – Sudhir Bastakoti Mar 18 '13 at 10:00
  • 1
    It's not resource type but an object of type `SimpleXMLElement`. Just FYI. And it can not be serialized as the error message says. But it supports casting to string (which resource types to not support that way). – hakre Mar 19 '13 at 10:12
  • This was so helpful - what I don't understand is why is this not an error 500 - rather, my Firefox just says page cannot be displayed. I only found this error deep in my error log! #HairPullout – James Wilson Sep 09 '14 at 10:43