I am sending an XML URL to return some data which I have managed to display as a multiple array. I am finding it difficult to separate some specific data and was hoping somebody might be able to teach me how to add this data to a variable properly with an example.
Here is the full array using print_r();
SimpleXMLElement Object ( [@attributes] => Array ( [Status] => OK ) [Errors] => SimpleXMLElement Object ( ) [Warnings] => SimpleXMLElement Object ( ) [RequestedCommand] => namecheap.domains.check [CommandResponse] => SimpleXMLElement Object ( [@attributes] => Array ( [Type] => namecheap.domains.check ) [DomainCheckResult] => SimpleXMLElement Object ( [@attributes] => Array ( [Domain] => test.com [Available] => false [ErrorNo] => 0 [Description] => [IsPremiumName] => false [PremiumRegistrationPrice] => 0 [PremiumRenewalPrice] => 0 [PremiumRestorePrice] => 0 [PremiumTransferPrice] => 0 [IcannFee] => 0 [EapFee] => 0 ) ) ) [Server] => PHX01APIEXT01 [GMTTimeDifference] => --4:00 [ExecutionTime] => 0.045 )
I can usually manage arrays okay, although it is the '@attributes' here which are tripping me up... This is my script so far (bare in mind I am simply testing here).
$xml = simplexml_load_string($data);
print_r($xml);
echo '<br><br>';
$status = current($xml->attributes());
$results = $xml->DomainCheckResult->attributes();
echo $status['Status'];
echo '<br><br>';
print_r($results);
The $status variable is working and echo's 'OK'.
The $results variable only seems to show 'SimpleXMLElement Object ( )'
I am trying to aim for these specifically -
[Domain] => test.com [Available] => false [ErrorNo] => 0 [Description] => [IsPremiumName] => false [PremiumRegistrationPrice] => 0 [PremiumRenewalPrice] => 0 [PremiumRestorePrice] => 0 [PremiumTransferPrice] => 0 [IcannFee] => 0 [EapFee] => 0
Yet I can't seem to get much further than I already am? How would I use this data to have something similar to the following...
$domain_req = $xml[Domain];
$domain_avail = $xml[Available];
And so on...?
As requested, here is the original XML -
<ApiResponse xmlns="http://api.namecheap.com/xml.response" Status="OK">
<Errors/>
<Warnings/>
<RequestedCommand>namecheap.domains.check</RequestedCommand>
<CommandResponse Type="namecheap.domains.check">
<DomainCheckResult Domain="test.com" Available="false" ErrorNo="0" Description="" IsPremiumName="false" PremiumRegistrationPrice="0" PremiumRenewalPrice="0" PremiumRestorePrice="0" PremiumTransferPrice="0" IcannFee="0" EapFee="0"/>
</CommandResponse>
<Server>PHX01APIEXT03</Server>
<GMTTimeDifference>--4:00</GMTTimeDifference>
<ExecutionTime>0.018</ExecutionTime>
</ApiResponse>
print_r.