Bild: Clker-Free-Vector-Images, Pixabay Lizenz

Aktuelle Wechselkurse der EZB parsen

Die EZB stellt eine Vielzahl von statistischen Kennzahlen bereit, die man nicht nur direkt downloaden kann, sondern auch in eigenen Anwendungen nutzen kann. Einen Überblick über alle Services der EZB gibt es hier. Dazu gehören auch die wichtigsten Wechselkurse für gängige Währungen, wie US-Dollar (USD), Pfund Sterling (GBP) oder Japanische Yen (JPY). Die Daten werden auch als XML bereitgestellt und können mit wenigen Zeilen Code geparst werden.

<?php 

$url = "https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml";
$xmlRaw = file_get_contents($url);

$doc = new DOMDocument();
$doc->preserveWhiteSpace = FALSE;
$doc->loadXML($xmlRaw);

$nodes = $doc->getElementsByTagName('Cube')->item(0);

$data = array();
foreach ($nodes->childNodes as $node) {

    foreach ($node->childNodes as $item) {
        $time = $node->getAttribute('time');
        $currency = $item->getAttribute('currency');
        $rate = $item->getAttribute('rate');

        $data[$currency] = array("time" => $time,"rate" => $rate);
    }
}

echo "<pre>" . print_r($data) . "</pre>";
?>

Ausgabe

Array
(
    [USD] => Array
        (
            [time] => 2024-01-29
            [rate] => 1.0823
        )

    [JPY] => Array
        (
            [time] => 2024-01-29
            [rate] => 160.13
        )

    [BGN] => Array
        (
            [time] => 2024-01-29
            [rate] => 1.9558
        )

    [CZK] => Array
        (
            [time] => 2024-01-29
            [rate] => 24.806
        )

    [DKK] => Array
        (
            [time] => 2024-01-29
            [rate] => 7.4538
        )

    [GBP] => Array
        (
            [time] => 2024-01-29
            [rate] => 0.85250
        )

    [HUF] => Array
        (
            [time] => 2024-01-29
            [rate] => 390.03
        )

    [PLN] => Array
        (
            [time] => 2024-01-29
            [rate] => 4.3660
        )

    [RON] => Array
        (
            [time] => 2024-01-29
            [rate] => 4.9773
        )

    [SEK] => Array
        (
            [time] => 2024-01-29
            [rate] => 11.3562
        )

    [CHF] => Array
        (
            [time] => 2024-01-29
            [rate] => 0.9339
        )

    [ISK] => Array
        (
            [time] => 2024-01-29
            [rate] => 148.50
        )

    [NOK] => Array
        (
            [time] => 2024-01-29
            [rate] => 11.3050
        )

    [TRY] => Array
        (
            [time] => 2024-01-29
            [rate] => 32.8509
        )

    [AUD] => Array
        (
            [time] => 2024-01-29
            [rate] => 1.6398
        )

    [BRL] => Array
        (
            [time] => 2024-01-29
            [rate] => 5.3134
        )

    [CAD] => Array
        (
            [time] => 2024-01-29
            [rate] => 1.4542
        )

    [CNY] => Array
        (
            [time] => 2024-01-29
            [rate] => 7.7702
        )

    [HKD] => Array
        (
            [time] => 2024-01-29
            [rate] => 8.4549
        )

    [IDR] => Array
        (
            [time] => 2024-01-29
            [rate] => 17127.13
        )

    [ILS] => Array
        (
            [time] => 2024-01-29
            [rate] => 3.9834
        )

    [INR] => Array
        (
            [time] => 2024-01-29
            [rate] => 89.9995
        )

    [KRW] => Array
        (
            [time] => 2024-01-29
            [rate] => 1445.60
        )

    [MXN] => Array
        (
            [time] => 2024-01-29
            [rate] => 18.5591
        )

    [MYR] => Array
        (
            [time] => 2024-01-29
            [rate] => 5.1236
        )

    [NZD] => Array
        (
            [time] => 2024-01-29
            [rate] => 1.7696
        )

    [PHP] => Array
        (
            [time] => 2024-01-29
            [rate] => 61.053
        )

    [SGD] => Array
        (
            [time] => 2024-01-29
            [rate] => 1.4516
        )

    [THB] => Array
        (
            [time] => 2024-01-29
            [rate] => 38.487
        )

    [ZAR] => Array
        (
            [time] => 2024-01-29
            [rate] => 20.3022
        )

)