我一直在谷歌搜索超过一个小时而且很沮丧,这看起来很简单。我试图打印的只是我的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数组。我不确定我做错了什么
答案 0 :(得分:3)
也许$ aid = $ xml-> accounts-> account-> accountId;?
答案 1 :(得分:1)
如果在响应中有可能获得多个帐户,您可以向帐户元素添加数组索引:
$aid = $xml->accounts->account[0]->accountId;
或迭代帐户:
foreach ($xml->accounts->account as $account) {
...
}
另请注意,$ aid的类型为SimpleXMLElement。在大多数情况下,您可以按原样使用它,它将自动转换为适当的类型,但如果您希望将值作为字符串,则可以使用显式转换:
$aid = (string) $xml->accounts->account[0]->accountId;