我正在寻找可以在JavaScript 或 Python 中使用的 API,以便能够交换货币汇率。 我也需要使用这些 API 的示例。 你们有任何想法如何找到它吗? JavaScript 或 Python 投资组合和职位示例?
3 回答
试试CurrencyFreaks API。它以 JSON/XML 格式为全球 179 种货币提供最新的货币兑换,兼容各种编程语言,包括 Javascript 和 PHP。
使用 Javascript 进行货币转换:
var xhr = new XMLHttpRequest();
xhr.withCredentials = true;
xhr.addEventListener("readystatechange", function() {
if(this.readyState === 4) {
console.log(this.responseText);
}
});
xhr.open("GET", "https://api.currencyfreaks.com/latest/convert
?apikey=YOUR_APIKEY
&from=USD&to=EUR
&amount=500");
xhr.send();
JSON 响应将是:
{
"date": "2020-10-16 15:08:00+00",
"current_rates": {
"USD": "1.0",
"EUR": "0.8533"
},
"converted_amount": "426.6215",
"query": {
"given_amount": "500.0",
"from": "USD",
"to": "EUR"
}
}
使用 PHP 进行货币转换:
setUrl('https://api.currencyfreaks.com/latest/convert
?apikey=YOUR_APIKEY
&from=USD&to=EUR
&amount=500');
$request->setMethod(HTTP_Request2::METHOD_GET);
$request->setConfig(array(
'follow_redirects' => TRUE
));
try {
$response = $request->send();
if ($response->getStatus() == 200) {
echo $response->getBody();
}
else {
echo 'Unexpected HTTP status: ' . $response->getStatus() . ' ' .
$response->getReasonPhrase();
}
}
catch(HTTP_Request2_Exception $e) {
echo 'Error: ' . $e->getMessage();
}
JSON 响应将是相同的。
我希望它对你有用。
要检索当前汇率,您可以使用 Yahoo Finance API ( http://finance.yahoo.com/d/quotes.csv?e=.csv&f=sl1d1t1&s=USDINR=X
)。
要检索特定历史日期的货币相对于美元的汇率,您可以使用 Yahoo currency-converter-cache API ( http://finance.yahoo.com/connection/currency-converter-cache?date=<YYYYMMDD>
)。
我制作了这个简单的货币转换器模块,它使用 Yahoo Finance API 并支持历史日期。https://github.com/iqbalhusen/currency_converter。
Finance.yahoo.com 的链接失效了!
可以使用: http: //free.currencyconverterapi.com/api/v5/convert?q=EUR_USD &compact=y