<?php
/*
+--------------------------------------------------------------------------+
| Author : Ciprian Voicu |
| Email : pictoru at autoportret dot ro |
| URL : http://www.autoportret.ro |
+--------------------------------------------------------------------------+
| Name : Pictoru's BNR Currency Parser |
| Filename : PBNR.inc.php |
| Description : Retrieve information about some important currencies |
| from the National Bank of Romania site. |
| The class return the information as an array with |
| 2 elements: date and currencies. |
| For PHP4 works with DOM XML class, and for PHP5 with |
| DOM class. |
| Created Date : 2008-02-11 |
| Last Modified : 2008-03-18 |
| Version : 1.1 |
+--------------------------------------------------------------------------+
| License : Free to use for anyone |
+--------------------------------------------------------------------------+
*/
#
class PBNR{
var $BNRString;
var $BNRList;
#
function PBNR(){
$this->BNRString=file_get_contents('http://www.bnro.ro/nbrfxrates.xml');
$this->BNRList=$this->parse();
}
#
function parse(){
if(function_exists('domxml_open_mem')){
return $this->withDOMXML();
}else{
return $this->withDOM();
}
}
function get($what=NULL){
if(empty($what))
return $this->BNRList;
else
$what=strtolower($what);
if($what=='date')
return $this->BNRList['date'];
else if(in_array($what,array_keys($this->BNRList['currencies'])))
return $this->BNRList['currencies'][$what];
else
return $this->BNRList;
}
#
function withDOMXML(){
$xml=domxml_open_mem($this->BNRString);
$root = $xml->document_element();
#
$data_el=$root->get_elements_by_tagname('Cube');
$obj['date']=$data_el[0]->get_attribute('date');
#
$data_elements=$data_el[0]->get_elements_by_tagname('Rate');
foreach($data_elements as $key=>$arr){
$element=$data_elements[$key];
$k=strtolower($element->get_attribute('currency'));
$v=$element->child_nodes();
$obj['currencies'][$k]=$v[0]->node_value();
if($element->has_attribute('multiplier')){
$m=$element->get_attribute('multiplier');
$obj['currencies'][$k]=$v[0]->node_value()/$m;
}
}
#
return $obj;
}
#
function withDOM(){
if(ini_get('zend.ze1_compatibility_mode')){
ini_set('zend.ze1_compatibility_mode','0');
$ze1_compatibility_mode=true;
}
$xml=new DOMDocument();
$xml->loadXML($this->BNRString);
$data_el=$xml->getElementsByTagName('Cube');
$obj['date']=$data_el->item(0)->getAttribute('date');
#
$data_elements=$xml->getElementsByTagName('Rate');
foreach($data_elements as $item){
$k=strtolower($item->getAttribute('currency'));
$obj['currencies'][$k]=($item->hasAttribute('multiplier')?$item->nodeValue/$item->getAttribute('multiplier'):$item->nodeValue);
}
if($ze1_compatibility_mode)
ini_set('zend.ze1_compatibility_mode','1');
return $obj;
}
}
?>