hedera-web/rest/misc/exchange-rate.php

42 lines
1005 B
PHP
Raw Normal View History

<?php
/**
* Ejemplo:
* <Cube><Cube time="2010-12-10"><Cube currency="USD" rate="1.3244"/>
2017-09-13 07:51:57 +00:00
*/
2018-05-23 10:14:20 +00:00
class ExchangeRate extends Vn\Lib\Method {
function run($db) {
$db->selectDb('vn2008');
// Indica la URL del archivo
2018-05-23 10:14:20 +00:00
$xml = new SimpleXMLElement(
'http://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist-90d.xml', 0, TRUE);
2018-05-23 10:14:20 +00:00
$date = $db->getValue("SELECT MAX(date) fecha FROM reference_rate");
$maxDate = $date ? DateTime::createFromFormat('Y-m-d', $date) : NULL;
2018-05-23 10:14:20 +00:00
foreach($xml->Cube[0]->Cube as $cube) {
$xmlDate = new DateTime($cube['time']);
2017-09-13 07:51:57 +00:00
// Si existen datos más recientes de la máxima fecha los añade
2017-09-13 07:51:57 +00:00
if ($maxDate <= $xmlDate)
2018-05-23 10:14:20 +00:00
foreach($cube->Cube as $subCube)
if ($subCube['currency'] == 'USD') {
$params = [
'date' => $xmlDate,
'rate' => $subCube['rate']
];
2018-05-23 10:14:20 +00:00
$db->query(
'REPLACE INTO reference_rate(moneda_id, date, rate)
VALUES(2, #date, #rate)',
$params
);
}
}
2018-05-23 10:14:20 +00:00
$db->queryFromFile(__DIR__.'/exrate-add');
}
}