【问题标题】:Google Finance API in JavaScript or Python to get Currency exchange rateJavaScript 或 Python 中的 Google Finance API 获取货币汇率
【发布时间】:2012-01-04 06:41:52
【问题描述】:

我正在寻找可以在 JavaScript 或 Python 中使用的 API,以便能够交换货币汇率。 我也需要使用这些 API 的示例。 你们有任何想法如何找到它吗? JavaScript 或 Python 投资组合和职位示例?

【问题讨论】:

标签: position gdata portfolio google-finance


【解决方案1】:

试试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 响应将是相同的。

我希望它对你有用。

【讨论】:

    【解决方案2】:

    要检索当前汇率,您可以使用 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.

    【讨论】:

    • @DJClayworth 谢谢。我已经编辑了我的答案以包含基本部分。
    【解决方案3】:

    finance.yahoo.com 的链接失效了!

    可以使用: http://free.currencyconverterapi.com/api/v5/convert?q=EUR_USD&compact=y

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多