Using the code below, I get an XML with the exchange rate on a given date. However, sometimes there are no quotes for a given date and an error is returned to me. In such cases, I would like to do minus one day by a given date and get the data again. If there is another error, again minus one day. As soon as the correct XML is obtained, we cache it (now I cache everything received for 6 hours).
function get_currency($currency_code, $setDate, $format)
{
$date = $setDate;
$cache_time_out = '21600';
$filename = date('d-m-Y', strtotime($date)) . '.xml';
$file_currency_cache = __DIR__ . '/cache/' . $filename;
if (!is_file($file_currency_cache) || filemtime($file_currency_cache) < (time() - $cache_time_out)) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.cbr-xml-daily.ru/archive/' . $date . '/daily_utf8.xml');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
$out = curl_exec($ch);
curl_close($ch);
file_put_contents($file_currency_cache, $out);
}
$content_currency = simplexml_load_file($file_currency_cache);
return number_format(str_replace(',', '.', $content_currency->xpath('Valute[CharCode="' . $currency_code . '"]')[0]->Value), $format);
}
I show the result of parsing on the site like this:
$date = '2014/06/02';
$ex_rate_usd = get_currency('USD', $date, 3);
echo $ex_rate_usd;
I found this code on the web and it works correctly, but problems with the lack of currency quotes for some dates break everything. I will be grateful for any help in this matter.
$date = 2014/06/02;would give 167.83333333333 and not a date.$date = '2014/06/02';