PHP 货币转换
2021-04-26 20:28
标签:tps tin response als change bsp sts nts color 使用以下代码,可以获取当天的汇率进行货币转换 PHP 货币转换 标签:tps tin response als change bsp sts nts color 原文地址:https://www.cnblogs.com/ryanzheng/p/12218370.htmlfunction convert_currency($number, $currency) {
// Fetching JSON
$req_url = ‘https://api.exchangerate-api.com/v4/latest/USD‘;
$response_json = file_get_contents($req_url);
// Continuing if we got a result
if (false !== $response_json) {
// Try/catch for json_decode operation
try {
// Decoding
$response_object = json_decode($response_json);
if (!property_exists($response_object->rates, $currency)) {
return 0;
}
return round($number / $response_object->rates->$currency, 2);
}
catch (Exception $e) {
return 0;
}
}
}
echo convert_currency(100, "CNY");