使用 PHP 进行货币转换 [英] Currency Conversion using PHP

查看:31
本文介绍了使用 PHP 进行货币转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法,可以在网站上将任何金额从一种货币转换为另一种货币.用户将输入类似100"的内容并选择美元作为货币,然后选择澳元或加元作为要转换的货币.当他点击转换"按钮时,我想通过一些 API 自动转换该金额,并向他显示他选择转换为的货币金额.

I'm looking for a way to convert any amount from one currency to another on a website. The user would enter something like '100' and select USD as the currency, and then chooses Australian or Canadian dollars as the currency to convert to. When he clicks the 'Convert' button, I'd like to convert that amount automatically, through some API, and show him the amount in the currency he chose to convert to.

有什么想法吗?

推荐答案

找了很多,找到了这个.

After searching a lot, found this.

// 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);

    // YOUR APPLICATION CODE HERE, e.g.
    $base_price = 12; // Your price in USD
    $EUR_price = round(($base_price * $response_object->rates->EUR), 2);

    }
    catch(Exception $e) {
        // Handle JSON parse error...
    }
}

这工作正常.片段来自:https://www.exchangerate-api.com/docs/php-currency-api

This is working fine. The snippet is from: https://www.exchangerate-api.com/docs/php-currency-api

这篇关于使用 PHP 进行货币转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆