用php解析api url调用返回的xml结果时出错


Error parsing xml returned results from api url call with php

我在这里缺少什么?我得到的是"Location: 0"

ini_set("user_agent","Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1");
$url = "http://ebird.org/ws1.1/data/notable/region/recent?rtype=subnational1&r=US-AZ";
$xml = simplexml_load_file($url);
$locname = $xml->response->result->sighting->loc-id;
echo "Location: ".$locname . "<br/>";


问题是"-"因为php认为你想从$xml->response->result->sighting->loc中减去id

解决方案是更改:

$locname = $xml->response->result->sighting->loc-id;

$locname = $xml->result[0]->sighting[0]->{'loc-id'};

it work with me

我希望这对你有帮助

注意:我删除response节点,因为它是根,我选择第一个元素,因为文件包含许多节点