We help IT Professionals succeed at work.

unserialize(): Node no longer exists

hankknight
hankknight asked
on
Why doesn't this work?
<?php
header('Content-Type: text/plain; charset=UTF-8');
$xml = file_get_contents('http://pastehtml.com/view/19qgkgx.txt');
$s = simplexml_load_string($xml);
$z = serialize($s->ResultSet->Matchup->Trends->Team[0]->TeamTrends);
$q = unserialize($z);
print_r($q);
?>

Open in new window

Another example, same problem:
<pre><?php
$s = unserialize('O:16:"SimpleXMLElement":2:{s:11:"@attributes";a:3:{s:8:"TeamName";s:16:"Seattle Mariners";s:6:"TeamId";s:2:"27";s:8:"Location";s:4:"Away";}s:9:"Last5Game";a:5:{i:0;O:16:"SimpleXMLElement":11:{s:8:"GameDate";s:10:"06/13/2010";s:3:"Day";s:3:"Sun";s:5:"Score";s:1:"4";s:8:"Opponent";s:5:"at SD";s:13:"OpponentScore";s:1:"2";s:6:"Result";s:1:"W";s:9:"MoneyLine";s:4:"-115";s:9:"OverUnder";s:1:"6";s:7:"Starter";s:15:"Felix Hernandez";s:9:"StarterIP";s:4:"8.67";s:15:"OpposingStarter";s:9:"C Richard";}i:1;O:16:"SimpleXMLElement":11:{s:8:"GameDate";s:10:"06/12/2010";s:3:"Day";s:3:"Sat";s:5:"Score";s:1:"1";s:8:"Opponent";s:5:"at SD";s:13:"OpponentScore";s:1:"7";s:6:"Result";s:1:"L";s:9:"MoneyLine";s:3:"105";s:9:"OverUnder";s:1:"6";s:7:"Starter";s:5:"C Lee";s:9:"StarterIP";s:1:"7";s:15:"OpposingStarter";s:9:"W LeBlanc";}i:2;O:16:"SimpleXMLElement":11:{s:8:"GameDate";s:10:"06/11/2010";s:3:"Day";s:3:"Fri";s:5:"Score";s:1:"3";s:8:"Opponent";s:5:"at SD";s:13:"OpponentScore";s:1:"4";s:6:"Result";s:1:"L";s:9:"MoneyLine";s:4:"-140";s:9:"OverUnder";s:3:"6½";s:7:"Starter";s:8:"J Vargas";s:9:"StarterIP";s:1:"6";s:15:"OpposingStarter";s:9:"K Correia";}i:3;O:16:"SimpleXMLElement":11:{s:8:"GameDate";s:10:"06/10/2010";s:3:"Day";s:3:"Thu";s:5:"Score";s:1:"3";s:8:"Opponent";s:6:"at TEX";s:13:"OpponentScore";s:2:"12";s:6:"Result";s:1:"L";s:9:"MoneyLine";s:4:"-210";s:9:"OverUnder";s:2:"10";s:7:"Starter";s:15:"R Rowland-Smith";s:9:"StarterIP";s:4:"5.67";s:15:"OpposingStarter";s:8:"T Hunter";}i:4;O:16:"SimpleXMLElement":11:{s:8:"GameDate";s:10:"06/09/2010";s:3:"Day";s:3:"Wed";s:5:"Score";s:1:"2";s:8:"Opponent";s:6:"at TEX";s:13:"OpponentScore";s:2:"12";s:6:"Result";s:1:"L";s:9:"MoneyLine";s:4:"-210";s:9:"OverUnder";s:3:"9½";s:7:"Starter";s:7:"I Snell";s:9:"StarterIP";s:4:"1.67";s:15:"OpposingStarter";s:11:"C.J. Wilson";}}}');
print_r($s);
?>
</pre>

Open in new window

Comment
Watch Question

unserialise needs you to "cast" to type or else you get a string. So convert the XML fragment to a string first with ->asXML() and then after unserializing it use simplexml_load_string to recreate it.

Inelegant but......

<?php
header('Content-Type: text/plain; charset=UTF-8');
$xml = file_get_contents('http://pastehtml.com/view/19qgkgx.txt');
$s = simplexml_load_string($xml);

// The changed bits 
$z = serialize($s->ResultSet->Matchup->Trends->Team[0]->TeamTrends->asXml());
$q = simplexml_load_string( (string) unserialize($z) );


print_r($q);
?>


Produces

SimpleXMLElement Object
(
    [Trend] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [DisplayText] => The total has gone OVER in 4 of Seattle's last 5 games 
                )

            [1] => SimpleXMLElement Object
                (
                    [DisplayText] => Seattle is 1-5 SU in its last 6 games 
                )

            [2] => SimpleXMLElement Object
                (
                    [DisplayText] => Seattle is 1-5 SU in its last 6 games on the road
                )

            [3] => SimpleXMLElement Object
                (
                    [DisplayText] => The total has gone OVER in 4 of Seattle's last 5 games on the road
                )

            [4] => SimpleXMLElement Object
                (
                    [DisplayText] => Seattle is 1-4 SU in its last 5 games when playing St. Louis
                )

        )

)

Open in new window