kkreft
2/27/2014 - 1:57 PM

PHP NBP

<?php
defined('_JEXEC') or die('Direct Acces Denied');
define('PATH', '/var/www/bssieradz.pl/modules/mod_nbp_currency/');
function parseXML($content, $currencies) {
  
	$xml = new SimpleXMLElement($content);
	$c = array();
	foreach ($xml->pozycja as $pozycja) {
		if(in_array($pozycja->kod_waluty, $currencies)) {
			$c[trim($pozycja->kod_waluty)] = trim($pozycja->kurs_sredni);
		}
	}
	return $c;
}
function get_data($url) {
	$ch = curl_init();
	$timeout = 5;
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
	$data = curl_exec($ch);
	curl_close($ch);
	return $data;
}
 
 
$currency	= 'EUR,USD,CHF,GBP';//$params->get('currency');
$currencies = explode(',', $currency);
$oldCurrencies = json_decode(file_get_contents(PATH.'cache/currencies.json'), true);
$diff = array_diff($currencies, $oldCurrencies);
if(count($diff) == 0) {
	$diff = array_diff($oldCurrencies, $currencies);
}
 
$daysBack = 0;
$time = time() ;
while(true) {
	$dateToday		= date("ymd", $time - 60*60*24*$daysBack);
	$dateYesterday	= date("ymd", $time - 60*60*24 - 60*60*24*$daysBack);
	
	if(!file_exists(PATH.'cache/'.$dateToday.'.json') || !file_exists(PATH.'cache/'.$dateYesterday.'.json') || $diff != array() ) {
		$urlList = 'http://www.nbp.pl/kursy/xml/dir.txt';
		$list = get_data($urlList);
		$files = explode("\n", $list);
		$filesNumber = count($files);
 
		
		$fileToday = null;
		$fileYesterday = null;
		for($i = $filesNumber-2; $i >= 0; $i--) {
			if(strpos($files[$i], $dateToday) && $files[$i][0] == 'a') {
				$fileToday = trim($files[$i]).'.xml';
			}
			if(strpos($files[$i], $dateYesterday) && $files[$i][0] == 'a') {
				$fileYesterday = trim($files[$i]).'.xml';
			}
			if($fileToday !== null && $fileYesterday !== null) {
				break;
			}
		}
 
		if($fileToday !== null && $fileYesterday !== null) {
			$today = get_data('http://www.nbp.pl/kursy/xml/' . $fileToday);
			$yesterday = get_data('http://www.nbp.pl/kursy/xml/' . $fileYesterday);
			
			file_put_contents(PATH.'cache/'.$dateToday.'.json', json_encode(parseXML($today, $currencies)));
			file_put_contents(PATH.'cache/'.$dateYesterday.'.json', json_encode(parseXML($yesterday, $currencies)));
			
			$diff = array();
		} else {
			$daysBack++;
		}
		
	} else {
		break;
	}
}
 
file_put_contents(PATH.'cache/currencies.json', json_encode($currencies));
$today = json_decode(file_get_contents(PATH.'cache/'.$dateToday.'.json'), true);
$yesterday = json_decode(file_get_contents(PATH.'cache/'.$dateYesterday.'.json'), true);
?>
<ul>
<?php foreach($today as $c => $v): ?>
<li class="<?php echo strtolower($c); ?>">
	<span class="currency">
		<?php echo $c; ?>
	</span>
	<span class="value">
		<?php echo $v; ?>
	</span>
	<?php
	if($v > $yesterday[$c]) {
		$t = 'gt';
	} elseif($v < $yesterday[$c]) {
		$t = 'lt';
	} else {
		$t = 'eq';
	}
	$diff = round(str_replace(',', '.', $v) - str_replace(',', '.', $yesterday[$c]), 4)*100; 
	?>
	<span class="change <?php echo $t; ?>">
		<?php echo $diff; ?>%
	</span>
</li>
<?php endforeach; ?>
</ul>