skip to the main content area of this page
 
 
 
PHP: Preluarea cursului valutar de pe infovalutar.ro
 
1. Parsing XML
 
1. Citirea cursului unei monede la o anumita data
 
Inchide
 
1. Parsing XML [ top ]
 
 
Fisierul XML care va fi citit este http://infovalutar.ro/curs.rss
 
<?php
	$insideitem = false;
	$tag = "";
	$title = "";
	$description = "";
	$link = "";
	$curs = "";

	function startElement($parser, $name, $attrs) {
		global $insideitem, $tag, $title, $description, $link;
		if ($insideitem) {
			$tag = $name;
		} elseif ($name == "ITEM") {
			$insideitem = true;
		}
	}

	function endElement($parser, $name) {
		global $insideitem, $tag, $title, $description, $link, $curs;
		if ($name == "ITEM") {
			$key = htmlspecialchars(trim($title));
			$val = htmlspecialchars(trim($description));
			$curs["$key"] = $val;
			$title = "";
			$description = "";
			$link = "";
			$insideitem = false;
		}
	}

	function characterData($parser, $data) {
		global $insideitem, $tag, $title, $description, $link;
		if ($insideitem) {
		     switch ($tag) {
		          case "TITLE":
		               $title .= $data;
		               break;
		          case "DESCRIPTION":
		               $description .= $data;
		               break;
		          case "LINK":
		               $link .= $data;
		               break;
		     }
		}
	}

	$xml_parser = xml_parser_create();
	xml_set_element_handler($xml_parser, "startElement", "endElement");
	xml_set_character_data_handler($xml_parser, "characterData");
	
	$fp = fopen("http://www.infovalutar.ro/curs.rss","r");
	if (!$fp) {
		echo "Error opening XML file";
		exit;
	}
		
	while ($data = fread($fp, 4096))
		xml_parse($xml_parser, $data, feof($fp));	
		
	fclose($fp);
	xml_parser_free($xml_parser);
	
	print "<pre>";
	print_r($curs);
	print "</pre>";
	
	/* OUTPUT :
	Array
	(
	    [USD] => 3.0100
	    [EUR] => 3.5983
	    [XDR] => 4.3267
	    [XAU] => 45.3186
	    [TRY] => 2.2065
	    [SEK] => 0.3799
	    [PLN] => 0.926
	    [NOK] => 0.4601
	    [MDL] => 0.2375
	    [JPY] => 2.5975
	    [HUF] => 0.014285
	    [GBP] => 5.253
	    [EGP] => 0.5229
	    [DKK] => 0.4821
	    [CZK] => 0.1208
	    [CHF] => 2.3144
	    [CAD] => 2.5437
	    [AUD] => 2.2478
	)
	*/
	
?>
 
2. Citirea cursului unei monede la o anumita data [ top ]
 
-> Pentru a citi cursul valutar al unei monede la o anumita data, se citeste http://infovalutar.ro/AN/LUNA/ZI/MONEDA.bnr
 
-> Pentru a citi cursul valutar curent al unei monede, se citeste http://infovalutar.ro/azi/MONEDA.bnr
 
<?php
	function GetCurs($y, $m, $d, $moneda) {
		$fp = fopen("http://www.infovalutar.ro/$y/$m/$d/$moneda.bnr", "r");
		if (!$fp) 
			return -2;
			
		$curs = fread($fp, 4096);		
		fclose($fp);
		
		if (strpos($curs, "found")) 
			return -1;
			
		return $curs;
	}

	$y = "2005";
	$m = "10";
	$d = "12";
	$moneda = "EUR";	
	$eur = GetCurs($y, $m, $d, $moneda);
	if ($eur==-2)
		echo "Error reading file";
	else 
	if ($eur==-1)
		echo "Value not found for $moneda on $d-$m-$y";
	else 
		echo "Curs $moneda la data $d-$m-$y: $eur";
	
	$y = "2008";
	$m = "10";
	$d = "12";
	$moneda = "EUR";	
	$eur = GetCurs($y, $m, $d, $moneda);
	if ($eur==-2)
		echo "<br>Error reading file";
	else 
	if ($eur==-1)
		echo "<br>Value not found for $moneda on $d-$m-$y";
	else 
		echo "<br>Curs $moneda la data $d-$m-$y: $eur";
		
	/* OUTPUT : 
	
	Curs EUR la data 12-10-2005: 3.5906
	Value not found for EUR on 12-10-2008
	
	*/
?>
 
 
 
 
Resurse pentru preluat cursurile BNR/BCE
de catre programatori / site-uri / oameni
obsolete
  • cursuri.rss
  • rss.aspx