我正在寻找一种方法,可以在网站上将任何金额从一种货币转换为另一种货币.用户输入类似“100”的内容并选择USD作为货币,然后选择澳元或加元作为转换的货币.当他点击“转换”按钮时,我想通过某些API自动转换该金额,并向他显示他选择转换为的货币金额.
有任何想法吗?
解决方法:
此方法使用Yahoo货币API
完整教程:Currency Converter in PHP, Python, Javascript and jQuery
function currencyConverter($currency_from, $currency_to, $currency_input) {
$yql_base_url = "http://query.yahooapis.com/v1/public/yql";
$yql_query = 'select * from yahoo.finance.xchange where pair in ("' . $currency_from . $currency_to . '")';
$yql_query_url = $yql_base_url . "?q=" . urlencode($yql_query);
$yql_query_url .= "&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys";
$yql_session = curl_init($yql_query_url);
curl_setopt($yql_session, CURLOPT_RETURNTRANSFER, true);
$yqlexec = curl_exec($yql_session);
$yql_json = json_decode($yqlexec, true);
$currency_output = (float) $currency_input * $yql_json['query']['results']['rate']['Rate'];
return $currency_output;
}
$currency_input = 2;
//currency codes : http://en.wikipedia.org/wiki/ISO_4217
$currency_from = "USD";
$currency_to = "INR";
$currency = currencyConverter($currency_from, $currency_to, $currency_input);
echo $currency_input . ' ' . $currency_from . ' = ' . $currency . ' ' . $currency_to;