简单的xml对象

时间:2009-11-24 10:01:22

标签: php session

你好我试图将SimpleXMLElement的值保存到$ _SESSION,但这些值输入为“SimpleXMLElement Object”。代码如下:

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

结果示例

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

我想要

Array
(
[userName] => garfx

)

2 个答案:

答案 0 :(得分:5)

SimpleXML元素可以用作字符串,但是您需要将它们“转换”为字符串。

在PHP中进行转换是通过在数据类型前加上值

来完成的

所以,例如,

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

$bar设为包含字符“1”的字符串。

上述解决方案将是: -

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

答案 1 :(得分:1)

将其投放为(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;