我正在尝试从ECB货币XML对象中提取交换值。 可以在这里 http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml
我可以毫无问题地加载对象,print_r会显示所有数据。 但是,我无法从此对象获取特定数据。
$xml_url = "http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml";
if (($response_xml_data = file_get_contents($xml_url))===false){
echo "Error fetching XML\n";
} else {
libxml_use_internal_errors(true);
$data = simplexml_load_string($response_xml_data);
if (!$data) {
echo "Error loading XML\n";
foreach(libxml_get_errors() as $error) {
echo "\t", $error->message;
}
} else {
echo 'type is: '.gettype($data).'<br>';
echo $data->propertyArray;
//echo $data->asXML();
print_r($data);
}
}
这是print_r的输出
SimpleXMLElement对象([Cube] =&gt; SimpleXMLElement对象([Cube] =&gt; SimpleXMLElement对象([@attributes] =&gt;数组([时间] =&gt; 2018-03-09)[立方体] =&gt ; Array([0] =&gt; SimpleXMLElement Object([@attributes] =&gt; Array([currency] =&gt; USD [rate] =&gt; 1.2291))[1] =&gt; SimpleXMLElement对象([@attributes] =&gt;数组([货币] =&gt; JPY [费率] =&gt; 131.31))[2] =&gt; SimpleXMLElement对象([@attributes] =&gt;数组([货币] =&gt; BGN [费率] = &gt; 1.9558))[3] =&gt; SimpleXMLElement对象([@attributes] =&gt;数组([货币] =&gt; CZK [费率] =&gt; 25.454))[4] =&gt; SimpleXMLElement对象([@ attributes] =&gt;数组([货币] =&gt; DKK [费率] =&gt; 7.4494))[5] =&gt; SimpleXMLElement对象([@attributes] =&gt;数组([货币] =&gt;英镑[费率] ] =&gt; 0.88893))[6] =&gt; SimpleXMLElement对象([@attributes] =&gt;数组([货币] =&gt; HUF [费率] =&gt; 311.93))[7] =&gt; SimpleXMLElement对象( [@attributes] =&gt ;数组([货币] =&gt; PLN [费率] =&gt; 4.2018))[8] =&gt; SimpleXMLElement Object([@attributes] =&gt; Array([currency] =&gt; RON [rate] =&gt; 4.6570))[9] =&gt; SimpleXMLElement Object([@attributes] =&gt; Array([currency] =&gt; SEK [rate] =&gt; 10.1648))[10] =&gt; SimpleXMLElement Object([@attributes] =&gt; Array([currency] =&gt; CHF [rate] =&gt; 1.1695))[11] =&gt; SimpleXMLElement Object([@attributes] =&gt; Array([currency] =&gt; ISK [rate] =&gt; 122.90))[12] =&gt; SimpleXMLElement Object([@attributes] =&gt; Array([currency] =&gt; NOK [rate] =&gt; 9.5948))[13] =&gt; SimpleXMLElement对象([@attributes] =&gt;数组([货币] =&gt; HRK [费率] =&gt; 7.4355))[14] =&gt; SimpleXMLElement Object([@attributes] =&gt; Array([currency] =&gt; RUB [rate] =&gt; 70.1112))[15] =&gt; SimpleXMLElement Object([@attributes] =&gt; Array([currency] =&gt; TRY [rate] =&gt; 4.6939))[16] =&gt; SimpleXMLElement Object([@attributes] =&gt; Array([currency] =&gt; AUD [rate] =&gt; 1.5772))[17] =&gt; SimpleXMLElement对象([@attributes] =&gt;数组([货币] =&gt; BRL [费率] =&gt; 4.0100))[18] =&gt; SimpleXMLElement Object([@attributes] =&gt; Array([currency] =&gt; CAD [rate] =&gt; 1.5848))[19] =&gt; SimpleXMLElement对象([@attributes] =&gt;数组([货币] =&gt; CNY [费率] =&gt; 7.7895))[20] =&gt; SimpleXMLElement Object([@attributes] =&gt; Array([currency] =&gt; HKD [rate] =&gt; 9.6370))[21] =&gt; SimpleXMLElement Object([@attributes] =&gt; Array([currency] =&gt; IDR [rate] =&gt; 16976.78))[22] =&gt; SimpleXMLElement Object([@attributes] =&gt; Array([currency] =&gt; ILS [rate] =&gt; 4.2345))[23] =&gt; SimpleXMLElement Object([@attributes] =&gt; Array([currency] =&gt; INR [rate] =&gt; 80.1315))[24] =&gt; SimpleXMLElement Object([@attributes] =&gt; Array([currency] =&gt; KRW [rate] =&gt; 1315.51))[25] =&gt; SimpleXMLElement对象([@attributes] =&gt;数组([货币] =&gt; MXN [费率] =&gt; 22.8810))[26] =&gt; SimpleXMLElement对象([@attributes] =&gt;数组([货币] =&gt; MYR [费率] =&gt; 4.8105))[27] =&gt; SimpleXMLElement Object([@attributes] =&gt; Array([currency] =&gt; NZD [rate] =&gt; 1.6931))[28] =&gt; SimpleXMLElement Object([@attributes] =&gt; Array([currency] =&gt; PHP [rate] =&gt; 64.102))[29] =&gt; SimpleXMLElement Object([@attributes] =&gt; Array([currency] =&gt; SGD [rate] =&gt; 1.6204))[30] =&gt; SimpleXMLElement对象([@attributes] =&gt;数组([货币] =&gt; THB [费率] =&gt; 38.538))[31] =&gt; SimpleXMLElement对象([@attributes] =&gt;数组([货币] =&gt; ZAR [费率] =&gt; 14.6257))))))
如何从美元获得汇率的价值? 看完这个帖子后 How can I access an array/object? 我仍然不知道如何访问数据。
echo $data->propertyArray;
不会返回任何内容。
答案 0 :(得分:4)
您可以迭代对象并检查每种类型的货币。
$xml_url = "http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml";
if (($response_xml_data = file_get_contents($xml_url))===false){
echo "Error fetching XML\n";
} else {
libxml_use_internal_errors(true);
$data = simplexml_load_string($response_xml_data);
if (!$data) {
echo "Error loading XML\n";
foreach(libxml_get_errors() as $error) {
echo "\t", $error->message;
}
} else {
foreach($data->Cube->Cube->Cube as $currency) {
if($currency['currency'] == 'USD') {
echo $currency['rate'];
}
}
}
}