Tutorial: Creare script curs valutar in PHP


In acest tutorial va voi prezenta un cod PHP care va permite afisarea pe site-ul dumneavoastra a cursului valutar preluat de la BNR.

Acest cod PHP va permite afisarea cursului oficial BNR pentru EURO si DOLAR.

 <?
class cursBnrXML
{
var $xmlDocument = “”;

var $date = “”;

var $currency = array();
function cursBnrXML($url)
{
$this->xmlDocument = file_get_contents($url);
$this->parseXMLDocument();
}
function parseXMLDocument()
{
$xml = new SimpleXMLElement($this->xmlDocument);

$this->date=$xml->Header->PublishingDate;

foreach($xml->Body->Cube->Rate as $line)
{
$this->currency[]=array(“name”=>$line[“currency”], “value”=>$line, “multiplier”=>$line[“multiplier”]);
}
}
function getCurs($currency)
{
foreach($this->currency as $line)
{
if($line[“name”]==$currency)
{
return $line[“value”];
}
}

return “Incorrect currency!”;
}
}
$curs=new cursBnrXML(“http://www.bnr.ro/nbrfxrates.xml”);
echo ‘<div style=”font-size:10px”>tutorialeit.net</div>’;
echo “<table style=’font-size:12px; border:#CCCCCC 1px solid;’><tr><td align=’center’>Curs Valutar<br />”.$curs->date.”</td></tr>
<tr><td><table style=’font-size:12px; border:#CCCCCC 1px solid;’><tr><td>USD:</td><td>”.$curs->getCurs(‘USD’).”</td></tr></table></td></tr>
<tr><td><table style=’font-size:12px; border:#CCCCCC 1px solid;’><tr><td>EUR:</td><td>”.$curs->getCurs(‘EUR’).”</td></tr></table></td></tr>

</table>”;
?>

Sper sa va foloseasca acest script php pentru afisarea cursului BNR.



Leave a Reply

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