_url = $url; } public function getExchangeRates() { $xml = $this->_getXml(); $p = xml_parser_create(); xml_parse_into_struct($p, $xml, $vals, $index); xml_parser_free($p); $nbp = array(); foreach($index['NAZWA_WALUTY'] as $value) { $nbp[$vals[$value+4]['value']] = str_replace(',', '.', $vals[$value+6]['value']); } return $nbp; } public function get($code) { $rates = $this->getExchangeRates(); if (!isset($rates[$code])) { throw new Exception('Currency does not exist!'); } return $rates[$code]; } public function getEuro() { return $this->get('EUR'); } private function _getXml() { $filepath = sprintf('%s/%s.xml', $this->_cache, date('Y-m-d')); if (file_exists($filepath)) { return file_get_contents($filepath); } else { $html = file_get_contents($this->_url . '/kursy/kursya.html'); $pattern = '/\/kursy\/xml\/(.*?)\.xml/'; preg_match_all($pattern, $html, $matches); if (!isset($matches[0][0])) { throw new Exception('Unable to find XML filepath!'); } $xml = file_get_contents($this->_url . $matches[0][0]); if (!file_exists($this->_cache)) { if (!mkdir($this->_cache)) { throw new Exception('Unable to create cache directory!'); } } if (!file_put_contents($filepath, $xml)) { throw new Exception('Unable to save file in cache directory!'); } return $xml; } } } $nbp = new Nbp(); echo $nbp->getEuro(); echo $nbp->get('USD'); print_r($nbp->getExchangeRates()); ?>