При получении курса волюты с сайта центрального банка существует несколько нюансов. Один из них — кэширование полученных результатов. В моем случае значение цены выводилось много раз, и при простом подходе вело к повторным запросам на сайт cbr.ru, чтобы этого избежать я поместил полученные значения в сессию. Это весьма простое и слегка топорное, но все таки решение.
Сниппет для MODX — getCurRate
if(empty($_SESSION['cbr_eur']) || empty($_SESSION['cbr_usd'])) {
$xml = simplexml_load_string(file_get_contents("http://www.cbr.ru/scripts/XML_daily.asp"));
$json = json_encode($xml);
$array = json_decode($json);
if(empty($input)){
$input = 0;
}
$input = $input;
$eur = 0;
$usd = 0;
foreach($array->Valute as $v){
if(strtolower($v->CharCode) == "eur"){
$eur = $v->Value;
}
if(strtolower($v->CharCode) == "usd"){
$usd = $v->Value;
}
}
$_SESSION['cbr_eur'] = $eur;
$_SESSION['cbr_usd'] = $usd;
}else{
$eur = $_SESSION['cbr_eur'];
$usd = $_SESSION['cbr_usd'];
}
if(strtolower($cur_input) == "rub"){
if(strtolower($cur_output) == "eur"){
return $input/$eur;
}
if(strtolower($cur_output) == "usd"){
return $input/$usd;
}
}
if(strtolower($cur_input) == "eur"){
if(strtolower($cur_output) == "rub"){
return $input*$eur;
}
if(strtolower($cur_output) == "usd"){
return $input*($eur/$usd);
}
}
if(strtolower($cur_input) == "usd"){
if(strtolower($cur_output) == "rub"){
return $input*$usd;
}
if(strtolower($cur_output) == "eur"){
return $input/($eur/$usd);
}
}
if(strtolower($cur_output) == "rubinusd"){
return $usd;
}
if(strtolower($cur_output) == "rubineur"){
return $eur;
}
return "getCurRate"; //Если что-то пошло не так
Использование сниппета getCurRate
[[!getCurRate?&cur_output=`rubinusd`]]— Курс USD[[!getCurRate?&cur_output=`rubineur`]]— Курс EUR[[!getCurRate?&input=`2700`&cur_input=`rub`&cur_output=`usd`]]— Переводо RUB в USD[[!getCurRate?&input=`132`&cur_input=`usd`&cur_output=`rub`]]— Переводо USD в RUB
Использование инструмента вне MODX
функцию().
Показать
function getCurRate($input = 0, $cur_input = '', $cur_output = ''){
if(empty($_SESSION['cbr_eur']) || empty($_SESSION['cbr_usd'])) {
$xml = simplexml_load_string(file_get_contents("http://www.cbr.ru/scripts/XML_daily.asp"));
$json = json_encode($xml);
$array = json_decode($json);
$eur = 0;
$usd = 0;
foreach($array->Valute as $v){
if(strtolower($v->CharCode) == "eur"){
$eur = $v->Value;
}
if(strtolower($v->CharCode) == "usd"){
$usd = $v->Value;
}
}
$_SESSION['cbr_eur'] = $eur;
$_SESSION['cbr_usd'] = $usd;
}else{
$eur = $_SESSION['cbr_eur'];
$usd = $_SESSION['cbr_usd'];
}
if(strtolower($cur_input) == "rub"){
if(strtolower($cur_output) == "eur"){
return $input/$eur;
}
if(strtolower($cur_output) == "usd"){
return $input/$usd;
}
}
if(strtolower($cur_input) == "eur"){
if(strtolower($cur_output) == "rub"){
return $input*$eur;
}
if(strtolower($cur_output) == "usd"){
return $input*($eur/$usd);
}
}
if(strtolower($cur_input) == "usd"){
if(strtolower($cur_output) == "rub"){
return $input*$usd;
}
if(strtolower($cur_output) == "eur"){
return $input/($eur/$usd);
}
}
if(strtolower($cur_output) == "rubinusd"){
return $usd;
}
if(strtolower($cur_output) == "rubineur"){
return $eur;
}
return "getCurRate"; //Если что-то пошло не так
}
<?php echo getCurRate(0, '', 'rubinusd'); ?>— Курс USD<?php echo getCurRate(0, '', 'rubineur'); ?>— Курс EUR<?php echo getCurRate(2700, 'rub', 'usd'); ?>— Переводо RUB в USD<?php echo getCurRate(132, 'usd', 'rub'); ?>— Переводо USD в RUB
Комментарии (5)
Не писать ответ