繁体   English   中英

从ECB货币XML对象提取数据

[英]Extract data from the ECB currency XML object

我正在尝试从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] => SimpleXMLElement对象([Cube] => SimpleXMLElement对象([@attributes] => Array([time] => 2018-03-09)[Cube] => Array([0] => SimpleXMLElement对象([@attributes] =>数组([currency] => USD [rate] => 1.2291))[1] => SimpleXMLElement Object([@attributes] => Array([currency] => JPY [rate] => 131.31))[2] => SimpleXMLElement对象([@attributes] =>数组([currency] => BGN [rate] => 1.9558))[3] => SimpleXMLElement对象([@attributes] => Array ([currency] => CZK [rate] => 25.454))[4] => SimpleXMLElement对象([@attributes] => Array([currency] => DKK [rate] => 7.4494))[5] => SimpleXMLElement对象([@attributes] =>数组([currency] => GBP [rate] => 0.88893))[6] => SimpleXMLElement Object([@attributes] => Array([currency] => HUF [rate] => 311.93))[7] => SimpleXMLElement对象([@attributes] =>数组([currency] => PLN [rate] => 4.2018))[8] => SimpleXMLElement对象([@attributes] => Array ([ currency] => RON [rate] => 4.6570))[9] => SimpleXMLElement对象([@attributes] =>数组([currency] => SEK [rate] => 10.1648))[10] => SimpleXMLElement对象([@attributes] =>数组([currency] => CHF [rate] => 1.1695))[11] => SimpleXMLElement对象([@attributes] => Array([currency] => ISK [rate] => 122.90))[12] => SimpleXMLElement对象([@attributes] =>数组([currency] => NOK [rate] => 9.5948))[13] => SimpleXMLElement对象([@attributes] => Array([ currency] => HRK [rate] => 7.4355))[14] => SimpleXMLElement对象([@attributes] => Array([currency] => RUB [rate] => 70.1112))[15] => SimpleXMLElement对象([@attributes] =>数组([currency] => TRY [rate] => 4.6939))[16] => SimpleXMLElement对象([@attributes] => Array([currency] => AUD [rate] => 1.5772))[17] => SimpleXMLElement对象([@attributes] =>数组([currency] => BRL [rate] => 4.0100))[18] => SimpleXMLElement对象([@attributes] => Array([货币] => CAD [r ate] => 1.5848))[19] => SimpleXMLElement对象([@attributes] =>数组([currency] => CNY [rate] => 7.7895))[20] => SimpleXMLElement对象([@attributes] = >数组([currency] => HKD [rate] => 9.6370))[21] => SimpleXMLElement对象([@attributes] => Array([currency] => IDR [rate] => 16976.78))[22] => SimpleXMLElement对象([@attributes] =>数组([currency] => ILS [rate] => 4.2345))[23] => SimpleXMLElement对象([@attributes] => Array([currency] => INR [ rate] => 80.1315))[24] => SimpleXMLElement对象([@attributes] =>数组([currency] => KRW [rate] => 1315.51))[25] => SimpleXMLElement对象([@attributes] = >数组([currency] => MXN [rate] => 22.8810))[26] => SimpleXMLElement对象([@attributes] => Array([currency] => MYR [rate] => 4.8105))[27] => SimpleXMLElement对象([@attributes] =>数组([currency] => NZD [rate] => 1.6931)))[28] => SimpleXMLElement对象([@attributes] => Array([currency] => PHP [率] => 64.102 ))[29] => SimpleXMLElement对象([@attributes] =>数组([currency] => SGD [rate] => 1.6204))[30] => SimpleXMLElement对象([@attributes] => Array([currency ] => THB [rate] => 38.538))[31] => SimpleXMLElement对象([@attributes] => Array([currency] => ZAR [rate] => 14.6257)))))))

我如何从美元中获取汇率的值? 阅读此线程后, 如何访问数组/对象? 我仍然不知道如何访问数据。

echo $data->propertyArray;

不返回任何东西。

您可以遍历对象并检查每种类型的货币。

$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'];
            }
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM