-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcurrency_rate_fetcher.php
executable file
·48 lines (33 loc) · 1.24 KB
/
currency_rate_fetcher.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/php
<?php
include('./vendor/autoload.php');
use Dotenv\Dotenv;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
$date = date('Y-m-d', time() - 24 * 60 * 60);
$conf = DotEnv::createImmutable(__DIR__);
$conf->load();
$logger = new Logger('CurrencyRateFetcher');
$logger->pushHandler(new StreamHandler($_ENV['LOGGER_STORAGE'], Logger::DEBUG));
$logger->debug("Fetching rates for $date: " . $_ENV['CBR_CURRENCY_RATE_URL']);
$client = new nusoap_client($_ENV['CBR_CURRENCY_RATE_URL'], 'wsdl');
$client->soap_defencoding = 'UTF-8';
$client->decode_utf8 = FALSE;
$response = $client->call($_ENV['CBR_CURRENCY_RATE_ACTION'], ['On_date' => $date]);
if (!empty($client->getError())) {
$logger->error($client->getError());
die($client->getError());
}
$result = $response['GetCursOnDateXMLResult']['ValuteData']['ValuteCursOnDate'];
if (empty($result)) {
$logger->error('Wrong result received from the CBR service');
die('Wrong result received from the CBR service');
}
$rates = [];
foreach($result as $item) {
$item['Vname'] = trim($item['Vname']);
array_push($rates, $item);
}
$jsonRates = json_encode($rates, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
$logger->info("JSON Rates: $jsonRates");
echo "$jsonRates\n";