Πώς να εξαγάγει τις συναλλαγματικές ισοτιμίες από την ιστοσελίδα του datafeed NBR

bnr_data_feed

Γεια σε όλους,

Μερικοί από τους φίλους μου με ρώτησε πώς μπορείτε να εμφανίσετε στις ιστοσελίδες τους τις συναλλαγματικές ισοτιμίες που προβλέπονται από την Εθνική Τράπεζα της Ρουμανίας, έτσι έχω αποφασίσει να κάνει ένα μικρό σεμινάριο γι 'αυτό για όποιον βρήκαν χρήσιμο.

Πρώτα απ 'όλα θα πρέπει να διαβάσετε την παρεχόμενη ζωοτροφών. Θα το κάνετε αυτό χρησιμοποιώντας τους ακόλουθους κωδικούς:

$szFeed = "http://www.bnro.ro/nbrfxrates.xml";
$arrXML = simplexml_load_file($szFeed);

Next, we will need a variable to hold our exchange rates.

$arrCurrency = array();

Assuming we have no errors so far we will have to parse the result hold in $arrXML.
We will do this with a “foreach” loop:

$nIndex = 0;
foreach($arrXML->Body->Cube->Rate as $rate)
{
	// we need a variable to store attributes for rate element
	$arrAttribute = array();
	// the rate element can have two possible attributes:
	// currency and multiplier as exemplified below
	// <Rate currency="GBP">4.7690</Rate>
	// <Rate currency="HUF" multiplier="100">1.4603</Rate>
        //
	$arrAttribute = $arrXML->Body->Cube->Rate[$nIndex]->attributes();
	//check to see if multiplier is present
	if ($arrAttribute[1] != null) $szMultiplier = " x " . $arrAttribute[1];
	else $szMultiplier = "";
	$arrCurrency["{$arrAttribute[0]}"] = $rate;
	// now print the currency
	echo "$nIndex - 1 $arrAttribute[0] $szMultiplier = $rate RON" . "<br />";
	$nIndex++;
}

If you wish to extract only the rate for EUR you get it from $arrCurrency["EUR"].

Off course you have to add some error checking on the way.
If you want to learn more about simplexml_load_file you can check the official php manual .

Download the source code here:  Source code for exchange rates tutorial .

Hope you liked it.

VN:F [1.9.11_1134]
Rating: 8.1/ 10 (14 votes cast)
How to extract the exchange rates from NBR's website datafeed , 8.1 out of 10 based on 14 ratings

About the Author

This website is about me, my experiences, my hobbies and my friends. If you are interested in my professional profile you can check it on LinkedIn.