SimpleXML打印键值问题

发布于 2024-11-30 05:34:59 字数 2240 浏览 0 评论 0原文

我已经在谷歌上搜索了一个多小时,很沮丧,这看起来很简单。我想要打印的只是我的 accountId。这是我从服务器返回的 xml:

SimpleXMLElement Object
(
[accounts] => SimpleXMLElement Object
    (
        [account] => SimpleXMLElement Object
            (
                [billingStreet] => SimpleXMLElement Object
                    (
                    )

                [billingCity] => SimpleXMLElement Object
                    (
                    )

                [billingState] => SimpleXMLElement Object
                    (
                    )

                [billingPostalCode] => SimpleXMLElement Object
                    (
                    )

                [billingCountry] => SimpleXMLElement Object
                    (
                    )

                [city] => Los Angeles
                [accountId] => XXXXX
                [companyName] => SimpleXMLElement Object
                    (
                    )

                [country] => United States
                [email] => XXXXX
                [enabled] => 1
                [fax] => SimpleXMLElement Object
                    (
                    )

                [firstName] => XXXXX
                [lastName] => XXXXX
                [multiClientFolder] => 0
                [multiUser] => 0
                [phone] => XXXXX
                [postalCode] => XXXXX
                [state] => CA
                [street] => XXXXX
                [title] => SimpleXMLElement Object
                    (
                    )

                [accountType] => 0
                [subscriberLimit] => 250000
            )

    )

[total] => 1
[limit] => 20
[offset] => 0
)

我想要的只是 accountId。我正在使用它,但它不打印任何内容:

$ch=curl_init("https://app.sandbox.icontact.com/icp/a/");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$buf = curl_exec($ch);
curl_close($ch);
$xml = simplexml_load_string($buf);
$aid=$xml->$accounts->$account->$accountId;
print($aid);

我可以使用 print_r 打印整个 xml 数组。我不确定我做错了什么

I have been searching google for over an hour and am frustrated, This seems so simple. All I am trying to print is my accountId. Here is the xml i am being returned from the server:

SimpleXMLElement Object
(
[accounts] => SimpleXMLElement Object
    (
        [account] => SimpleXMLElement Object
            (
                [billingStreet] => SimpleXMLElement Object
                    (
                    )

                [billingCity] => SimpleXMLElement Object
                    (
                    )

                [billingState] => SimpleXMLElement Object
                    (
                    )

                [billingPostalCode] => SimpleXMLElement Object
                    (
                    )

                [billingCountry] => SimpleXMLElement Object
                    (
                    )

                [city] => Los Angeles
                [accountId] => XXXXX
                [companyName] => SimpleXMLElement Object
                    (
                    )

                [country] => United States
                [email] => XXXXX
                [enabled] => 1
                [fax] => SimpleXMLElement Object
                    (
                    )

                [firstName] => XXXXX
                [lastName] => XXXXX
                [multiClientFolder] => 0
                [multiUser] => 0
                [phone] => XXXXX
                [postalCode] => XXXXX
                [state] => CA
                [street] => XXXXX
                [title] => SimpleXMLElement Object
                    (
                    )

                [accountType] => 0
                [subscriberLimit] => 250000
            )

    )

[total] => 1
[limit] => 20
[offset] => 0
)

All i want is accountId. I am using this and it doesnt print anything:

$ch=curl_init("https://app.sandbox.icontact.com/icp/a/");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$buf = curl_exec($ch);
curl_close($ch);
$xml = simplexml_load_string($buf);
$aid=$xml->$accounts->$account->$accountId;
print($aid);

i can print the entire xml array just fine with print_r though. Im not sure what i am doing wrong

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

醉生梦死 2024-12-07 05:34:59

也许 $aid=$xml->accounts->account->accountId;?

Maybe $aid=$xml->accounts->account->accountId;?

简美 2024-12-07 05:34:59

如果有可能在响应中获取多个帐户,您可以向帐户元素添加一个数组索引:

$aid = $xml->accounts->account[0]->accountId;

或者迭代帐户:

foreach ($xml->accounts->account as $account) {
    ...
}

另请注意,$aid 的类型为 SimpleXMLElement。在大多数情况下,您可以按原样使用它,它将自动转换为适当的类型,但如果您希望该值作为字符串,则可以使用显式转换:

$aid = (string) $xml->accounts->account[0]->accountId;

If there is the possibility of getting more than one account in the response, you can add an array index to the account element:

$aid = $xml->accounts->account[0]->accountId;

Or iterate over the accounts:

foreach ($xml->accounts->account as $account) {
    ...
}

Also note that $aid will be of type SimpleXMLElement. In most cases you can use this as-is and it will be automatically cast to the appropriate type, but if you want the value as a string you can use an explicit cast:

$aid = (string) $xml->accounts->account[0]->accountId;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文