使用以下代码,可以获取当天的汇率进行货币转换

function 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");

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-27
  • 2021-11-28
  • 2021-10-23
  • 2022-12-23
  • 2022-01-04
猜你喜欢
  • 2022-12-23
  • 2022-01-21
  • 2021-07-24
  • 2021-10-12
  • 2021-10-24
  • 2022-12-23
  • 2021-08-26
相关资源
相似解决方案