currencylayer:简单有效的货币转换

Every developer that's maintained an eCommerce site will tell you that being responsible for properly handling currency will tell you it can be a very stressful task.  If you write buggy or insecure code, you're going to cost either your employer or the user money.  An added complication for developers can be currency conversion if the app caters to an international audience.  The last thing developers need is a complicated API, especially one that aids them in handling currency.  That's where currencylayer comes in:  a simple, comprehensive API for currency conversion for your web and native apps!

每个维护了电子商务站点的开发人员都会告诉您,正确处理货币的责任将告诉您,这可能是一项非常艰巨的任务。 如果您编写错误的代码或不安全的代码,则将花费您的雇主或用户钱。 如果该应用程序适合国际受众,则开发人员可能会增加货币换算的麻烦。 开发人员最后需要的是一个复杂的API,尤其是可以帮助他们处理货币的API。 这就是currencylayer的用处:一个简单,全面的API,可为您的Web和本机应用程序转换货币!

快速点击 (Quick Hits)

Here are a few of the highlights of currencylayer's offering:

以下是Currencylayer产品的一些亮点:

  • Trusted by Disney Pixar, FedEx, Lyft, and MetLife

    受到迪士尼皮克斯,联邦快递,Lyft和MetLife的信任
  • Supports dozens of currencies

    支持多种货币
  • Supports bitcoin currency conversion

    支持比特币货币转换
  • Provides HTTPS API usage

    提供HTTPS API用法
  • Currency value updates every 60 seconds

    货币值每60秒更新一次
  • Clear documentation with numerous code samples (cURL, PHP, jQuery, etc.)

    包含大量代码示例(cURL,PHP,jQuery等)的清晰文档

  • Very competitive pricing

    极具竞争力的价格

使用currencylayer (Using currencylayer)

currencylayer offers free signup with a generous request allotment for testing.  After you sign up you'll be given an API key to kick off development.  Armed with an API key and the documentation, it's time to use currencylayer's API endpoints to experiment with currencies!

currencylayer提供免费注册以及大量测试请求。 注册后,您将获得一个API密钥以开始开发。 有了API密钥和文档,现在是时候使用currencylayer的API端点来试验货币了!

所有Currencylayer请求的提示 (Tips for All currencylayer Requests)

  • All requests must be GET requests

    所有请求必须是GET请求
  • The access_key parameter, which represents your API KEY, is required for all requests

    代表您的API KEY的access_key参数对于所有请求都是必需的

  • You can add format=1 to any request to request currenclylayer format the JSON response (nice!)

    您可以将format=1添加到任何请求以请求currenclylayer格式的JSON响应(很好!)

  • You can add source={code} to change the source currency from USD to whichever language you'd like

    您可以添加source={code}以将源货币从USD更改为您想要的任何一种语言

  • You can add currency={currencylist} to specify which currencies you want information for

    您可以添加currency={currencylist}来指定要获取哪些货币信息

  • currencylayer provides extensive code samples in their documentation

    currencylayer在其文档中提供了广泛的代码示例

获取货币清单 (Getting a Currency Listing)

A good first step is using the currency listing endpoint to see which currencies are supported by currencylayer:

第一步是使用货币列表终结点来查看currencylayer支持哪些货币:


# The "list" endpoint provides a basic list of supported currencies
curl http://apilayer.net/api/list?access_key=MY_API_KEY&format=1



// Response:
{
   "success":true,
   "terms":"https:\/\/currencylayer.com\/terms",
   "privacy":"https:\/\/currencylayer.com\/privacy",
   "currencies":{
      "AED":"United Arab Emirates Dirham",
      "AFN":"Afghan Afghani",
      "ALL":"Albanian Lek",
      "AMD":"Armenian Dram",
      "ANG":"Netherlands Antillean Guilder",
      "AOA":"Angolan Kwanza",
      "ARS":"Argentine Peso",
      "AUD":"Australian Dollar",
      "BTC":"Bitcoin", // Yay bitcoin!
      // ...
      "USD":"United States Dollar",
      "UYU":"Uruguayan Peso",
      "UZS":"Uzbekistan Som",
      "VEF":"Venezuelan Bol\u00edvar Fuerte",
      "VND":"Vietnamese Dong"
   }
}


currencylayer supports an exhaustive list of currencies, opening up your international reach! Also note that you can add format=1 to your request URL to have

currencylayer支持详尽的货币清单,可扩展您的国际影响力! 另请注意,您可以将format=1添加到您的请求网址中

获取实时汇率 (Getting Real-Time Rates)

Real-time rate information is likely the most popular use of a currency conversion service like currencylayer, especially when you're dealing with a volatile currency like bitcoin.  In the case of eCommerce sites, once you get the user's location (via IP comparison or user preference), localized sites will display a localized price.

实时汇率信息可能是诸如currencylayer之类的货币转换服务的最流行用法,尤其是在处理诸如比特币之类的易变货币时。 对于电子商务站点,一旦获得用户的位置(通过IP比较或用户首选项),本地化的站点将显示本地化的价格。

You can use the currencies key to get just the conversion you'd like

您可以使用currencies键来获取所需的转换


# Retrieve the value of USD compared to EUR (the Euro)
# If you don't provide a "currencies" key, all currencies will be returned
curl http://apilayer.net/api/live?format=1&currencies=EUR&access_key=MY_API_KEY



{
  "success":true,
  "terms":"https:\/\/currencylayer.com\/terms",
  "privacy":"https:\/\/currencylayer.com\/privacy",
  "timestamp":1494266647,
  "source":"USD",
  "quotes":{
    "USDEUR":0.914798
  }
}


Pulling currency rates "in bulk" is a smart way to limit API usage; currencylayer allows developers to grab multiple currency values at a time:

大量提高货币汇率是限制API使用的明智方法。 currencylayer允许开发人员一次获取多种货币值:


# Retrieve the value of USD compared to EUR (the Euro), GBP (Pound), BTC (Bitcoin)
curl http://apilayer.net/api/live?format=1&currencies=GBP,EUR,BTC&access_key=MY_API_KEY



{
  "success":true,
  "terms":"https:\/\/currencylayer.com\/terms",
  "privacy":"https:\/\/currencylayer.com\/privacy",
  "timestamp":1494266647,
  "source":"USD",
  "quotes":{
    "USDGBP":0.77271, // 1 USD is 0.77271 Pounds (GBP)
    "USDEUR":0.914798,
    "USDBTC":0.000628
  }
}


Unless you need to the moment accuracy, I'd recommend caching responses from currency layer to keep your system both speed and API usage efficient.  I love that currencylayer allows developers to grab information in bulk.

除非需要即时准确性,否则建议您从货币层缓存响应,以保持系统速度和API使用效率。 我喜欢currencylayer允许开发人员批量获取信息。

获取历史货币数据 (Getting Historical Currency Data)

Retrieving historical data is also a nice feature from currencylayer, especially when you're working with volatile currencies.  This feature is great for building charts based on currency values.

检索历史数据也是currencylayer的一项不错的功能,尤其是在使用易变货币时。 此功能非常适合基于货币值构建图表。


# Get the value of BTC one year ago
curl http://apilayer.net/api/historical?date=2016-05-08&currencies=BTC&format=1&access_key=MY_API_KEY

# Get the value of BTC two years ago
curl http://apilayer.net/api/historical?date=2015-05-08&currencies=BTC&format=1&access_key=MY_API_KEY



// 2016
{
  "success":true,
  "terms":"https:\/\/currencylayer.com\/terms",
  "privacy":"https:\/\/currencylayer.com\/privacy",
  "historical":true,
  "date":"2016-05-08",
  "timestamp":1462751999,
  "source":"USD",
  "quotes":{
    "USDBTC":0.002177
  }
}

// 2015
{
  "success":true,
  "terms":"https:\/\/currencylayer.com\/terms",
  "privacy":"https:\/\/currencylayer.com\/privacy",
  "historical":true,
  "date":"2015-05-08",
  "timestamp":1431129599,
  "source":"USD",
  "quotes":{
    "USDBTC":0.004093
  }
}


The historical data endpoint accepts a single date argument and returns currency value for each supported currency.

历史数据端点接受单个日期参数,并返回每种受支持货币的货币值。

使用JSONP (Using JSONP)

I'm a massive fan of JSONP; the number of times I've had to code a proxy to shim CORS has made me an annoyed, cynical developer.  currencylayer goes the extra mile to provide JSONP support:

我是JSONP的忠实拥护者 ; 我不得不编写代理以填充CORS的次数使我成为一个烦人,愤世嫉俗的开发人员。 currencylayer更加努力地提供JSONP支持:


// Get a conversion from USD to GBP for $20
$.ajax({
    url: 'http://apilayer.net/api/convert?access_key=MY_ACCESS_KEY&from=USD&to=GBP&amount=20,   
    dataType: 'jsonp',
    success: function(json) {
        // Update the pricing element with the GBP £ value
        document.querySelector('.price').html('£' + json.result);
    }
});


Thank you for providing JSONP support, currencylayer!

感谢您提供对JSONP的支持,currencylayer!

时间范围查询 (Time-Frame Queries)

Time-frame queries are super useful for knowing currency value change with set start and end dates.  One example could be Another great example is refunding bitcoin transactions; if you base your bitcoin on USD, and bitcoin went up significantly since the origin transaction, you'll want to know the BTC value at the time of sale and adjust the BTC amount refunded to the customer.

时间范围查询对于了解设置的开始日期和结束日期的货币价值变化非常有用。 一个很好的例子是另一个很好的例子是退款比特币交易。 如果您以美元为基础的比特币,并且自原始交易以来比特币大幅上涨,您将想知道出售时的比特币价值,并调整退还给客户的比特币金额。


# Customer wants a refund for a purchase made one month earlier
# In this case we need to figure out how much EUR has changed
curl http://apilayer.net/api/timeframe?start_date=2017-04-01&end_date=2017-05-01&currencies=EUR&format=1&access_key=MY_APK_KEY



{
  "success": true,
  "terms": "https://currencylayer.com/terms",
  "privacy": "https://currencylayer.com/privacy",
  "timeframe": true,
  "start_date": "2010-03-01",
  "end_date": "2010-04-01",
  "source": "USD",
  "quotes": {
    "2010-03-01": {
      "USDEUR": 0.738541
    },
    "2010-03-02": {
      "USDEUR": 0.736145
    },
    [...]
  }
}    


I also recommend checking out this Node.js resource which wraps currencylayer in a REST-like pattern, even allowing currency polling every x milliseconds!

我还建议您检查一下这个Node.js资源该资源将currencylayer以类似REST的模式包装,甚至允许每x毫秒轮询一次货币!

Developing a system or app that accepts money, much less requires currency conversion, can be incredibly stressful; the last thing developers need is an overcomplicated API to do the currency work.  The currencylayer API is one of the easiest to use APIs I've ever worked with.  After evaluating a few other likewise currency conversion services, I can also add that currencylayer appears to be the most reasonably priced, which is a huge bonus.  If you develop systems that require currency conversion, or want to add localized pricing, give currencylayer a shot.  Simple, cost-effective, and developer-friendly!

开发能够接受资金(更不用说需要货币兑换)的系统或应用程序可能会带来极大的压力; 开发人员需要做的最后一件事是使用过于复杂的API来完成货币工作。 currencylayer API是我曾经使用过的最容易使用的API之一。 在评估了其他一些类似的货币转换服务之后,我还可以添加一个货币层,似乎价格最合理,这是一笔巨大的奖励。 如果您开发的系统需要货币换算,或者要添加本地化的价格,请给Currencylayer试一下。 简单,经济,对开发人员友好!

翻译自: https://davidwalsh.name/currencylayer

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值