P
P
Pavel Chuev2018-03-07 13:33:53
PHP
Pavel Chuev, 2018-03-07 13:33:53

How to check if the Central Bank of the Russian Federation issued a forecast of the exchange rate for tomorrow?

Good day! On php I made a parser of the exchange rate from the Central Bank of the Russian Federation (forecasts for today and tomorrow).

spoiler
<?php

function get_rate($currency)
{

    $date = getdate();

    $day_of_the_week = $date['wday'];

    switch ($day_of_the_week) {
        case 0:
            $k1 = 2;
            $k2 = 1;
            break;   // воскресенье
        case 1:
            $k1 = 3;
            $k2 = 2;
            break;   // понедельник
        case 2:
            $k1 = 3;
            $k2 = 0;
            break;  // вторник
        default:
            $k1 = 1;
            $k2 = 0;
            break;   // среда, четверг, пятница, суббота
    }

    $month = $date['mon'];
    $day = $date['mday'];
    $yesterday = $day - $k1;
    $today = $day - $k2;
    $year = $date['year'];


    $date_yesterday = date("d/m/Y", mktime(0, 0, 0, $month, $yesterday, $year));
    $date_today = date("d/m/Y", mktime(0, 0, 0, $month, $today, $year));

    $rate = array();

    foreach ($currency as $key => $value) {
        $url = 'http://www.cbr.ru/scripts/XML_dynamic.asp?date_req1=' . $date_yesterday . '&date_req2=' . $date_today . '&VAL_NM_RQ=' . $value;
        $xml = simplexml_load_file($url);
        $rate_today = round(str_replace(',', '.', $xml->Record[1]->Value), 2);
        $rate_yesterday = round(str_replace(',', '.', $xml->Record[0]->Value), 2);
        $range = round($rate_today - $rate_yesterday, 2);

        if ($range > 0) {
            $range = '+' . $range;
            $img = 'plus';
        } elseif ($range == 0) {
            $range = 0;
            $img = 0;
        } else {
            $img = 'minus';
        }

        $rate[$key] = array(
            'today' => $rate_today,
            'change' => $range,
            'img' => $img
        );

    }

    return $rate;

}

function get_rate_tomorrow($currency)
{

    $date = getdate();

    $day_of_the_week = $date['wday'];

    switch ($day_of_the_week) {
        case 0:
            $k1 = 2;
            $k2 = 2;
            break;   // воскресенье
        case 1:
            $k1 = 3;
            $k2 = 1;
            break;   // понедельник
        case 2:
            $k1 = 3;
            $k2 = 1;
            break;  // вторник
        case 6:
            $k1 = 3;
            $k2 = 3;
            break; //суббота
        default:
            $k1 = 1;
            $k2 = 1;
            break;   // среда, четверг, пятница
    }

    $month = $date['mon'];
    $day = $date['mday'];
    $tomorrow = $day + $k2;
    $today = $day - $k1;
    $year = $date['year'];


    $date_tomorrow = date("d/m/Y", mktime(0, 0, 0, $month, $tomorrow, $year));
    $date_today = date("d/m/Y", mktime(0, 0, 0, $month, $today, $year));

    $rate2 = array();

    foreach ($currency as $key => $value) {
        $url = 'http://www.cbr.ru/scripts/XML_dynamic.asp?date_req1=' . $date_today . '&date_req2=' . $date_tomorrow . '&VAL_NM_RQ=' . $value;
        $xml = simplexml_load_file($url);
        $rate_today = round(str_replace(',', '.', $xml->Record[1]->Value), 2);
        $rate_tomorrow = round(str_replace(',', '.', $xml->Record[2]->Value), 2);
        $range = round($rate_tomorrow - $rate_today, 2);

        if ($range > 0) {
            $range = '+' . $range;
            $img = 'plus';
        } elseif ($range == 0) {
            $range = 0;
            $img = 0;
        } else {
            $img = 'minus';
        }
        $rate2[$key] = array(
            'tomorrow' => $rate_tomorrow,
            'change' => $range,
            'img' => $img
        );

    }

    return $rate2;

}

I call it all like this:
spoiler
<?php

require_once 'get_rate.php';

$currency = array(
    'dollar' => 'R01235',
    'euro' => 'R01239'
);

$rate = get_rate($currency);
$rate2 = get_rate_tomorrow($currency);

$date = getdate();

$day_of_the_week = $date['wday'];

//расчет коэффициента смещения
switch ($day_of_the_week) {
    case 0:
        $k1 = 2;
        $k2 = 2;
        break;   // воскресенье
    case 1:
        $k1 = 3;
        $k2 = 1;
        break;   // понедельник
    case 2:
        $k1 = 3;
        $k2 = 1;
        break;  // вторник
    case 6:
        $k1 = 3;
        $k2 = 3;
        break;
    default:
        $k1 = 1;
        $k2 = 1;
        break;   // среда, четверг, пятница, суббота
}

$month = $date['mon'];    // месяц
$day = $date['mday'];     // число сегодня
$tomorrow = $day + $k2;  // число для получени


$date_tomorrow = date("d.m", mktime(0, 0, 0, $month, $tomorrow)); // Генерация даты для курса завтрашнего дня
?>

And I output:
spoiler
<?php foreach ($rate2 as $key => $value) { ?>

                            <li>
                                <div class="image">
                                    <img src="images/<?php echo $key; ?>.png" alt="" width="43" height="44">
                                </div>
                                <div class="text-box">
                                    <strong class="price"><?php echo $value['tomorrow']; ?></strong>
                                    <?php if ($value['change']) { ?>

                                        <span class="difference difference-<?php echo $value['img']; ?>"><?php echo $value['change']; ?>%</span>
                                    <?php } else { ?>
                                        <span class="difference difference-plus">+0.00%</span>
                                    <?php } ?>
                                </div>
                            </li>

                        <?php } ?>

So, the Central Bank makes a forecast for tomorrow only during business hours (around lunchtime or later).
How to make it so that, provided that the xml response does not yet have a forecast for tomorrow, and display the old values? For example, today at 9 am there was no forecast yet, but the script accessed xml and returned zero values, and at 13:00 the forecast was added and everything fell into place.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
B
Boris Korobkov, 2018-03-07
@BorisKorobkov

How to make it so that, provided that ...

php.net/manual/en/control-structures.if.php

A
Alexander null, 2018-03-07
@snikes

https://currate.ru
Update rates every hour,
if you need exactly the Central Bank, make a request to the xml of the Central Bank, compare the values, if they have changed, then rewrite

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question