gareth
gareth

Reputation: 189

simple xml object

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

)

Upvotes: 0

Views: 129

Answers (2)

Mez
Mez

Reputation: 24953

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;

Upvotes: 5

Ben Everard
Ben Everard

Reputation: 13804

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;

Upvotes: 1

Related Questions