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

Your email address will not be published. Required fields are marked *

Skip to toolbar

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close