我使用的是一些第三方的API,其通過XML返回以下形式的錯誤:
<xml>
<status>0</status>
<error code="111">Error message text goes here.</error>
</xml>
在PHP中使用simplexml_load_string
我可以很容易地得到狀態0和錯誤消息文本,但我找不到從<error code="111">
檢索code="111"
值的方法。它似乎被SimpleXML丟棄。
<?php
$bytesRead = file_get_contents('http://api.....');
$xml = simplexml_load_string($bytesRead);
echo '<pre>'; print_r($xml); echo '</pre>';
?>
輸出
SimpleXMLElement Object
(
[status] => 0
[error] => Error message text goes here.
)
我缺少的東西?有沒有辦法獲得這個價值,或有人建議另一種方法來獲得這個?
這麼簡單,謝謝! –
拯救了生命! .............. –
['@attributes]'使用'$ obj-> attributes() - > status' –