1

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>
2
  • Can you show the original XML rather than the output or print_r. Commented Jul 31, 2017 at 8:27
  • @NigelRen I've updated my question Commented Jul 31, 2017 at 8:32

1 Answer 1

1

To access these, you should be using...

$status = current($xml->attributes());
$results = $xml->CommandResponse->DomainCheckResult->attributes();
echo $status['Status'];

$domain_req = $results['Domain'];
$domain_avail = $results['Available'];

You have to use quotes round the names.

Sign up to request clarification or add additional context in comments.

2 Comments

When I try to echo these, I get nothing? I'm not sure that my $results variable is set up correctly?
I've updated the answer, it was the $results, it was layered slightly differently. (also I did miss of the s in results).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.