Files
crm.clientright.ru/test/parser.php

51 lines
1.5 KiB
PHP
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
// Функция для получения HTML-кода страницы
function getCurrencyPage($date) {
$url = "https://cbr.ru/currency_base/daily/?UniDbQuery.Posted=True&UniDbQuery.To=" . $date;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$html = curl_exec($ch);
curl_close($ch);
return $html;
}
// Функция для парсинга HTML и извлечения курсов валют
function parseCurrencies($html) {
$doc = new DOMDocument();
@$doc->loadHTML($html);
$xpath = new DOMXPath($doc);
// Находим строки таблицы с курсами валют
$rows = $xpath->query('//table[contains(@class, "data")]//tr');
$currencies = [];
foreach ($rows as $row) {
$cols = $row->getElementsByTagName('td');
if ($cols->length > 0) {
$code = trim($cols->item(1)->nodeValue);
$rate = trim($cols->item(4)->nodeValue);
$currencies[$code] = $rate;
}
}
return $currencies;
}
// Используем функции
$date = "01.02.2024"; // Указанная дата
$html = getCurrencyPage($date);
$currencies = parseCurrencies($html);
// Выводим результат
echo "Курсы валют на дату $date:\n";
foreach ($currencies as $code => $rate) {
echo "$code: $rate\n";
}
?>