1
votes

I am trying to extract the exchange values from the ECB currency XML object. available here http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml

I can load the object without problem and a print_r shows me all the data. I am however unsuccessful at getting specific data from this object.

$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);

    }
}

this is the output from print_r

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

How can I get the value of the exchange rate for example from the USD ? After reading this thread How can I access an array/object? I still have no idea how to access the data.

echo $data->propertyArray;

doesn't return anything.

1
Nothing complicated, try for example var_dump($data->Cube->Cube->Cube[2]['rate']);, and take a look at the examples in the PHP manual.Casimir et Hippolyte

1 Answers

4
votes

You can iterate over the object and check for the currency of each type.

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