SimpleXML打印键值问题
我已经在谷歌上搜索了一个多小时,很沮丧,这看起来很简单。我想要打印的只是我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许 $aid=$xml->accounts->account->accountId;?
Maybe $aid=$xml->accounts->account->accountId;?
如果有可能在响应中获取多个帐户,您可以向帐户元素添加一个数组索引:
或者迭代帐户:
另请注意,$aid 的类型为 SimpleXMLElement。在大多数情况下,您可以按原样使用它,它将自动转换为适当的类型,但如果您希望该值作为字符串,则可以使用显式转换:
If there is the possibility of getting more than one account in the response, you can add an array index to the account element:
Or iterate over the accounts:
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: