tags:

views:

62

answers:

2

hello i'm trying to save the values of a SimpleXMLElement to a $_SESSION but the values are entered as "SimpleXMLElement Object". code below:

$xml = new SimpleXMLElement($auth_info);
$_SESSION[userName] = $xml->profile->preferredUsername; (garfx)
$_SESSION[email] =  $xml->profile->verifiedEmail;
$_SESSION[givenName] =  $xml->profile->name->givenName;
$_SESSION[lastName] =  $xml->profile->name->familyName;

results example

Array
(
[userName] => SimpleXMLElement Object
()
)

i would like

Array
(
[userName] => garfx

)
+4  A: 

SimpleXML elements can be used as strings, but you need to "cast" them to a string.

Casting in PHP is done by prefixing the data type to a value,

so for example,

$foo = 1;
$bar = (string)$foo;

Would make $bar be a string containing the character "1".

The solution for the above would be:-

$xml = new SimpleXMLElement($auth_info);
$_SESSION[userName] = (string)$xml->profile->preferredUsername; // (garfx)
$_SESSION[email] = (string)$xml->profile->verifiedEmail;
$_SESSION[givenName] = (string)$xml->profile->name->givenName;
$_SESSION[lastName] = (string)$xml->profile->name->familyName;
Mez
Good explanation +1
ILMV
+1  A: 

Cast it as a (string)

$xml = new SimpleXMLElement($auth_info);
$_SESSION[userName] = (string)$xml->profile->preferredUsername;
$_SESSION[email] = (string)$xml->profile->verifiedEmail;
$_SESSION[givenName] = (string)$xml->profile->name->givenName;
$_SESSION[lastName] = (string) $xml->profile->name->familyName;
ILMV