-
Notifications
You must be signed in to change notification settings - Fork 1
/
CursBNR.php
101 lines (85 loc) · 1.93 KB
/
CursBNR.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
/*
* Class CursBNR v1.02
* This class parses BNR's XML and returns the current exchange rate
*
* Requirements: PHP5
*
* Last update: October 2011, 27
* More info: https://www.curs-valutar-bnr.ro
*/
class CursBNR
{
/**
* xml document
* @var string
*/
var $xmlDocument = "";
/**
* exchange date
* BNR date format is Y-m-d
* @var string
*/
var $date = "";
/**
* currency
* @var associative array
*/
var $currency = array();
/**
* cursBnrXML class constructor
*
* @access public
* @param $url string
* @return void
*/
function __construct($url)
{
$this->xmlDocument = file_get_contents($url);
$this->parseXMLDocument();
}
/**
* parseXMLDocument method
*
* @access public
* @return void
*/
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"]);
}
}
/**
* getCurs method
*
* get current exchange rate: example getExchangeRate("USD")
*
* @access public
* @return double
*/
function getExchangeRate($currency)
{
foreach($this->currency as $line)
{
if($line["name"]==$currency)
{
return $line["value"];
}
}
return "Incorrect currency!";
}
}
//-----------------------------------------------------------------------------------------------------------------------------
//@an example of using the CursBNR class
$curs=new CursBNR("https://www.bnr.ro/nbrfxrates.xml");
print $curs->date;
print "<hr>";
print "USD: ".$curs->getExchangeRate("USD");
print "<hr>";
print "EUR: ".$curs->getExchangeRate("EUR");
print "<hr>";
?>