SimpleXML problem print key value
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] =>开发者_JS百科 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
Maybe $aid=$xml->accounts->account->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;
精彩评论