PHP simpleXML解析

FFi*_*ish 2 php simplexml nodes

我需要货币兑换,欧元兑换美元.
欧洲中央银行提供的费率如下:
http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml
我可以通过使用第一个节点获得美元汇率,但如果他们更改订单怎么办?
我需要更可靠的东西吗?我不知道怎么做..

$xml = @simplexml_load_file('http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml');
echo "dollar: " . $xml->Cube->Cube->Cube[0]->attributes()->rate;
Run Code Online (Sandbox Code Playgroud)

Jos*_*vis 6

只需使用XPath来获取属性@currency等于"USD"的任何节点,这样就可以了.

$xref  = simplexml_load_file('http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml');
$nodes = $xref->xpath('//*[@currency="USD"]');

echo $nodes[0]['rate'];
Run Code Online (Sandbox Code Playgroud)