game/_incl_data/crons/UpdateEkrPrices.php

29 lines
721 B
PHP
Raw Normal View History

<?php
use Core\{Config, Db};
/**
* Парсинг данных ЦБ РФ.
* https://www.cbr-xml-daily.ru/#howto
* Раз в день в час ночи.
*/
require_once dirname(__DIR__) . DIRECTORY_SEPARATOR . 'autoload.php';
$todayExchangeRate = new class {
private const API = 'https://www.cbr-xml-daily.ru/daily_json.js';
public static function getUSDEkrPrice(): float
{
$rates = json_decode(file_get_contents(self::API));
return round(Config::EKR_RUB_PRICE / $rates->Valute->USD->Value, 2);
}
};
$sql = 'insert into ekr_exchange_rates (RUB, USD) values (?,?)';
$args = [
round(Config::EKR_RUB_PRICE, 2),
$todayExchangeRate::getUSDEkrPrice(),
];
Db::sql($sql, $args);