我需要货币转换,欧元到美元。
欧洲中央银行在此处提供利率:
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;
最佳答案
只需使用 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'];
关于PHP简单XML解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2461757/