Created
February 9, 2021 16:47
-
-
Save evikza/65698b3eb57820c6c6201888f606826d to your computer and use it in GitHub Desktop.
Получения котировок ЦБ РФ в формате JSON с cbr.ru
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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