在调试时print_r和var_dump非常方便!例如在这种情况下,如果您在加载 $xml 后立即转储它,您会注意到您错过了 SimpleXMLElement Object 查询。
$url = "http://176.34.224.80/REMPADRecSys/GetVideosServlet?queryId=1";
$xml = simplexml_load_file($url);
echo "<pre>";
print_r($xml);
会给你输出:
SimpleXMLElement Object
(
[query] => SimpleXMLElement Object
(
[@attributes] => Array
(
[id] => 1
)
[results] => SimpleXMLElement Object
(
[item] => Array
(
[0] => SimpleXMLElement Object
(
[id] => GZ7w39jpqwo
[rank] => 1
[explanation] => Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla a massa lectus, sed convallis sapien. Curabitur sem mauris, ullamcorper ut.
)
所以正确的参考$xml->query->results->item[0]->id;
就像提到的@Lake。调试愉快。