从XML响应中访问Array部分的问题

时间:2017-07-31 08:01:59

标签: php arrays xml

我发送一个XML URL来返回一些我已设法显示为多个数组的数据。我发现很难分开一些特定的数据,并希望有人能够教我如何通过一个例子正确地将这些数据添加到变量中。

以下是使用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 ) 

我通常可以管理数组,虽然它是' @属性'在这里绊倒我...这是我的脚本到目前为止(记得我只是在这里测试)。

$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);

$status变量正在运行,并且回复了“确定”&#39;。 $results变量似乎只显示&#39; SimpleXMLElement对象()&#39;

我正在努力争取这些 -

 [Domain] => test.com [Available] => false [ErrorNo] => 0 [Description] => [IsPremiumName] => false [PremiumRegistrationPrice] => 0 [PremiumRenewalPrice] => 0 [PremiumRestorePrice] => 0 [PremiumTransferPrice] => 0 [IcannFee] => 0 [EapFee] => 0

然而,我似乎无法比现在更进一步?我如何使用这些数据来获得类似于以下内容...

$domain_req = $xml[Domain];
$domain_avail = $xml[Available];

等等......?

根据要求,这是原始的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>

1 个答案:

答案 0 :(得分:1)

要访问这些内容,您应该使用...

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

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

您必须在名称旁边使用引号。