我正在發送一個XML URL以返回一些我已經設法顯示爲多個數組的數據。我發現很難區分一些特定的數據,並希望有人能夠教我如何正確地將這些數據添加到變量中。從XML響應訪問數組部分的問題
下面是使用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
變量工作和回聲的 'OK'。 的$results
變量似乎只顯示「SimpleXMLElement對象()」
我試圖瞄準這些專門 -
[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>
可以顯示原始XML而不是輸出或'print_r'。 –
@NigelRen我已經更新了我的問題 – Snappysites