71 lines
2.2 KiB
PHP
71 lines
2.2 KiB
PHP
|
|
<?php
|
||
|
|
//crontab job - save currencies
|
||
|
|
|
||
|
|
function getCurrencies(){
|
||
|
|
//get xml file name from dir.txt
|
||
|
|
$today=date('ymd');
|
||
|
|
//$today="120127";
|
||
|
|
$connect = fopen("http://www.nbp.pl/kursy/xml/dir.txt", "r") or die("Błąd przy łączeniu");
|
||
|
|
while (!feof ($connect)) {
|
||
|
|
$buffer = fgets($connect, 4096) or die("Błąd przy odczycie
|
||
|
|
");
|
||
|
|
if ((substr($buffer,0,1)=='a') && (substr($buffer,5,6))==$today)
|
||
|
|
{
|
||
|
|
$xml_file = $buffer;
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
if (substr($buffer,0,1)=='a') $xml_file = $buffer; //get last "a" if file with "today" not exists
|
||
|
|
};
|
||
|
|
fclose($connect);
|
||
|
|
|
||
|
|
//download xml
|
||
|
|
$connect = fopen("http://www.nbp.pl/kursy/xml/".trim($xml_file).".xml", "r") or die("Błąd przy łączeniu");
|
||
|
|
$download = fopen("./currencies.xml", "w") or die("Błąd przy łączeniu");
|
||
|
|
while (!feof ($connect)) {
|
||
|
|
$buffer = fgets($connect, 4096);//or die("Błąd przy odczyciee");
|
||
|
|
fwrite($download, $buffer);// or die("Błąd przy zapisie");
|
||
|
|
}
|
||
|
|
fclose($connect);
|
||
|
|
fclose($download);
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
//load currencies
|
||
|
|
$db=$GLOBALS['superGlobalDb'];;
|
||
|
|
$currencies = array();
|
||
|
|
$result = $db->query("select id, iso4217 from currencies where deleted='0'");
|
||
|
|
while ($r=$db->fetchByAssoc($result)) {
|
||
|
|
$currencies[$r['iso4217']] = array();
|
||
|
|
$currencies[$r['iso4217']]['id']=$r['id'];
|
||
|
|
}
|
||
|
|
|
||
|
|
//read xml
|
||
|
|
$doc = new DOMDocument();
|
||
|
|
$doc->load( 'currencies.xml' );
|
||
|
|
$tmp = $doc->getElementsByTagName("data_publikacji");
|
||
|
|
$cur_date = $tmp->item(0)->nodeValue;
|
||
|
|
$tmp = $doc->getElementsByTagName("numer_tabeli");
|
||
|
|
$table_name = $tmp->item(0)->nodeValue;
|
||
|
|
$poz = $doc->getElementsByTagName("pozycja");
|
||
|
|
foreach ($poz as $p) {
|
||
|
|
$kod_waluty=$p->getElementsByTagName("kod_waluty");
|
||
|
|
$kod = $kod_waluty->item(0)->nodeValue;
|
||
|
|
if (isset($currencies[$kod])) {
|
||
|
|
$kurs_sredni=$p->getElementsByTagName("kurs_sredni");
|
||
|
|
$kurs = $kurs_sredni->item(0)->nodeValue;
|
||
|
|
$currencies[$kod]['kurs']=$kurs;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
var_dump($currencies);
|
||
|
|
//save values into db
|
||
|
|
foreach ($currencies as $key=>$value) {
|
||
|
|
$db->query("INSERT INTO currency_nbp_archive VALUES ('".$cur_date."', '".$value['id']."', '".$key."', ".str_replace(",",".",$value['kurs']).",'".$table_name."')");
|
||
|
|
|
||
|
|
//update nbp_conversion_rate in Currencies
|
||
|
|
$db->query("UPDATE currencies SET nbp_conversion_rate='".str_replace(",",".",$value['kurs'])."' WHERE id='".$value['id']."'");
|
||
|
|
}
|
||
|
|
|
||
|
|
//delete xls
|
||
|
|
unlink("./currencies.xml");
|
||
|
|
}
|
||
|
|
?>
|