Skip to content

Instantly share code, notes, and snippets.

@evikza
Created February 9, 2021 16:47
Show Gist options
  • Select an option

  • Save evikza/65698b3eb57820c6c6201888f606826d to your computer and use it in GitHub Desktop.

Select an option

Save evikza/65698b3eb57820c6c6201888f606826d to your computer and use it in GitHub Desktop.
Получения котировок ЦБ РФ в формате JSON с cbr.ru
<?php
$request = simplexml_load_string(
file_get_contents(
$uri = 'https://www.cbr.ru/scripts/XML_daily.asp',
false,
stream_context_create([
'http' => [
'method' => 'GET',
'header' =>
'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.146 Safari/537.36',
],
])
)
);
$currency = [];
$currentDate = strval($request['Date']);
foreach ($xml = $request->Valute as $value) {
$currency[strval($value->CharCode)] = floatval(
str_replace(',', '.', $value->Value)
);
}
echo json_encode(
[
'canonical' => $uri,
'date' => $currentDate,
'rates' => $currency,
],
0
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment