可能重复:
使用 PHP 中的 simplexml_load_string 函数,我可以轻松地获取状态 0 和错误消息文本,但是我找不到一种检索 中的 code="111" 值的方法。它似乎被 SimpleXML 忽略了。
输出
我有遗漏什么吗?有没有一种方法可以获得这个值,或者有没有人建议另一种获取它的方法?
可能重复:
从SimpleXML访问@属性
我正在使用一些第三方API,它们通过以下形式的XML返回错误:
<xml>
<status>0</status>
<error code="111">Error message text goes here.</error>
</xml>
使用 PHP 中的 simplexml_load_string 函数,我可以轻松地获取状态 0 和错误消息文本,但是我找不到一种检索 中的 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
。 - Bhavin Rana