Skip to content

Instantly share code, notes, and snippets.

@rakauchuk
Created January 16, 2015 21:21
Show Gist options
  • Save rakauchuk/96669527c72eb0ed5f5b to your computer and use it in GitHub Desktop.
Save rakauchuk/96669527c72eb0ed5f5b to your computer and use it in GitHub Desktop.
nbrb rates converted from xml to json (w/ mapping symbols)
<?php
try
{
header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json');
$ex = 'http://www.nbrb.by/Services/XmlExRates.aspx?ondate=' . date("m/d/Y");
$xml = @simplexml_load_file($ex);
if ($xml === false && !$xml instanceof SimpleXMLElement) {
throw new \Exception('Can not get currency list from nbrb');
}
$json = json_encode($xml);
$xmlArray = json_decode($json, true);
$result = array();
$signMap = json_decode('{"ALL": "Lek", "AFN": "؋", "ARS": "$", "AWG": "ƒ", "AUD": "$", "AZN": "ман", "BSD": "$", "BBD": "$", "BYR": "p.", "BZD": "BZ$", "BMD": "$", "BOB": "$b", "BAM": "KM", "BWP": "P", "BGN": "лв", "BRL": "R$", "BND": "$", "KHR": "៛", "CAD": "$", "KYD": "$", "CLP": "$", "CNY": "¥", "COP": "$", "CRC": "₡", "HRK": "kn", "CUP": "₱", "CZK": "Kč", "DKK": "kr", "DOP": "RD$", "XCD": "$", "EGP": "£", "SVC": "$", "EEK": "kr", "EUR": "€", "FKP": "£", "FJD": "$", "GHC": "¢", "GIP": "£", "GTQ": "Q", "GGP": "£", "GYD": "$", "HNL": "L", "HKD": "$", "HUF": "Ft", "ISK": "kr", "INR": "₹", "IDR": "Rp", "IRR": "﷼", "IMP": "£", "ILS": "₪", "JMD": "J$", "JPY": "¥", "JEP": "£", "KES": "KSh", "KZT": "лв", "KPW": "₩", "KGS": "лв", "LAK": "₭", "LVL": "Ls", "LBP": "£", "LRD": "$", "LTL": "Lt", "MKD": "ден", "MYR": "RM", "MUR": "₨", "MXN": "$", "MNT": "₮", "MZN": "MT", "NAD": "$", "NPR": "₨", "ANG": "ƒ", "NZD": "$", "NIO": "C$", "NGN": "₦", "NOK": "kr", "OMR": "﷼", "PKR": "₨", "PAB": "B/.", "PYG": "Gs", "PEN": "S/.", "PHP": "₱", "PLN": "zł", "QAR": "﷼", "RON": "lei", "RUB": "₽", "SHP": "£", "SAR": "﷼", "RSD": "Дин.", "SCR": "₨", "SGD": "$", "SBD": "$", "SOS": "S", "ZAR": "R", "KRW": "₩", "LKR": "₨", "SEK": "kr", "CHF": "Fr.", "SRD": "$", "SYP": "£", "TZS": "TSh", "TWD": "NT$", "THB": "฿", "TTD": "TT$", "TRY": "", "TRL": "₤", "TVD": "$", "UGX": "USh", "UAH": "₴", "GBP": "£", "USD": "$", "UYU": "$U", "UZS": "лв", "VEF": "Bs", "VND": "₫", "YER": "﷼", "ZWD": "Z$"}');
foreach ($xmlArray['Currency'] as $currency) {
$rates[$currency['CharCode']] = array(
"id" => isset($currency['NumCode']) ? $currency['NumCode'] : '',
"code" => isset($currency['CharCode']) ? $currency['CharCode'] : '',
"name" => isset($currency['Name']) ? $currency['Name'] : '',
"sign" => isset($signMap->$currency['CharCode']) ? $signMap->$currency['CharCode'] : '',
"scale" => isset($currency['Scale']) ? $currency['Scale'] : '',
"rate" => isset($currency['Rate']) ? $currency['Rate'] : ''
);
}
$result = array('rates' => $rates, 'errorCode' => 0, 'errorMessage' => '', 'statusCode' => '201', 'statusMessage' => 'Created');
http_response_code(201);
} catch (\Exception $e) {
$result = array('rates' => null, 'errorCode' => $e->getCode(), 'errorMessage' => $e->getMessage(), 'statusCode' => '404', 'statusMessage' => 'Not found');
http_response_code(404);
}
echo json_encode($result);
exit;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment