簡體   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