Curs BNR – PHP Script

Curs BNR - PHP Script

Curs BNR – PHP Script

 

Functia urmatoare returneaza cursul BNR pentru o valuta la o anumita data specificate.

// Curs BNR - PHP Script
function curs_BNR($currency, $date) {
    $year = date('Y', strtotime($date));
    $site = 'http://www.bnr.ro/files/xml/years/nbrfxrates' . $year . '.xml';
    $xmlstr = file_get_contents($site);
    if ($xmlstr != "") {
        $xml = new \SimpleXMLElement($xmlstr);
        for ($i = 1; $i < count($xml->Body[0]->Cube); $i++) {
            $D = $xml->Body[0]->Cube[$i]->attributes()['date'];
            if (strtotime($D) === strtotime($date)) {
                for ($j = 1; $j < count($xml->Body[0]->Cube[$i]->Rate); $j++) {
                    if ((string) $xml->Body[0]->Cube[$i]->Rate[$j]->attributes()['currency'] === (string) $currency) {
                        $curs = $xml->Body[0]->Cube[$i]->Rate[$j];
                    }
                }
            }
        }
        return isset($curs) ? $curs : null;
    }
}

Exemplu de apelare:

$euro = curs_BNR('EUR', '2017-06-15');
echo $euro;

Tipurile de valute le poti vedea pe http://www.bnr.ro/nbrfxrates.xml

Curs BNR – PHP Script




Curs BNR – PHP Script
Tagged on:             

Leave a Reply

Skip to toolbar