Php 基于Api的货币转换算法

Php 基于Api的货币转换算法,php,algorithm,currency,Php,Algorithm,Currency,我目前正在使用PHP开发一个应用程序,用户可以更改产品的货币,如易趣或aliexpress。因此,如果用户将其货币更改为美元,则所有产品的价格都将转换为美元 我已经搜索了一个API来获取名为CurrencyLayer的实时货币。API提供以下结构: "success": true, "terms": "https://currencylayer.com/terms", "privacy": "https://currencylayer.com/privacy", "timestamp

我目前正在使用PHP开发一个应用程序,用户可以更改产品的货币,如易趣或aliexpress。因此,如果用户将其货币更改为美元,则所有产品的价格都将转换为美元

我已经搜索了一个API来获取名为CurrencyLayer的实时货币。API提供以下结构:

"success": true,
  "terms": "https://currencylayer.com/terms",
  "privacy": "https://currencylayer.com/privacy",
  "timestamp": 1432480209,
  "source": "USD",
  "quotes": {
    "USDAED": 3.67315,
    "USDAFN": 60.790001,
    "USDALL": 126.194504,
    "USDAMD": 477.359985,
    "USDANG": 1.790403,
    [...]
  }
我的计划是每小时在我的数据库中保存这些引用。考虑到一个转换货币的函数,将一种货币转换为另一种货币的正确算法是什么?我知道这并不难,但我想不出来

function convertCurrency($currency1 = 'USD', $currency2 = 'EUR', $value){
   //Search the currency value and algorithm to convert   
   $newValue = (????)
   return $newValue;
}

请注意,由于结果看起来是JSON格式的,因此您可能首先需要调用结果以获得PHP对象格式的结果

json_解码后的API示例如下所示:

    // var_dump($api_result)
    stdClass Object
    (
        [success] => 1
        [terms] => https://currencylayer.com/terms
        [privacy] => https://currencylayer.com/privacy
        [timestamp] => 1432480209
        [source] => USD
        [quotes] => stdClass Object
            (
                [USDAED] => 3.67315
                [USDAFN] => 60.790001
                [USDALL] => 126.194504
                [USDAMD] => 477.359985
                [USDANG] => 1.790403
            )
    )
下一步是使用函数组合两个参数以访问(例如)USDAED结果:

<?php

    function convertCurrency($currency1 = 'USD', $currency2 = 'EUR', $value) {
        //Search the currency value and algorithm to convert
        $combined_currencies = $currency1.$currency2;
        return $api_result->quotes->$combined_currencies * $value;
    }

    echo convertCurrency("USD", "AED", 1); // 3.67315

快速提示,由于结果看起来是JSON格式的,您可能首先需要调用结果以获得PHP对象格式的结果

json_解码后的API示例如下所示:

    // var_dump($api_result)
    stdClass Object
    (
        [success] => 1
        [terms] => https://currencylayer.com/terms
        [privacy] => https://currencylayer.com/privacy
        [timestamp] => 1432480209
        [source] => USD
        [quotes] => stdClass Object
            (
                [USDAED] => 3.67315
                [USDAFN] => 60.790001
                [USDALL] => 126.194504
                [USDAMD] => 477.359985
                [USDANG] => 1.790403
            )
    )
下一步是使用函数组合两个参数以访问(例如)USDAED结果:

<?php

    function convertCurrency($currency1 = 'USD', $currency2 = 'EUR', $value) {
        //Search the currency value and algorithm to convert
        $combined_currencies = $currency1.$currency2;
        return $api_result->quotes->$combined_currencies * $value;
    }

    echo convertCurrency("USD", "AED", 1); // 3.67315

正如Gary Thomas已经提到的,CurrencyLayer API有一个源货币切换参数,允许您将基础货币从
USD
切换到
$currency1
参数设置为的任何货币

但是,据我所知,您希望能够定期查询CurrencyLayer API,仅使用
USD
作为源货币,并自己执行汇率计算

要实现这一点,您需要转换:

  • 货币1
    美元
  • 美元
    货币2
  • 将其转换为以下代码:

    函数convertCurrency($currency1,$currency2,$value)
    {
    $baseCurrency='USD';
    $quotes=[
    “USDCAD”=>1.28024,
    “USDEUR”=>0.838313,
    // ...
    ];
    $quote1=$quotes[$baseCurrency.$currency1];
    $quote2=$quotes[$baseCurrency.$currency2];
    返回$value/$quote1*$quote2;
    }
    兑换货币('EUR','CAD',10);//15.271622890257
    
    您还可以使用货币库,例如,为您处理这些计算(以及更多计算):

    使用Brick\Math\RoundingMode;
    使用Brick\Money\CurrencyConverter;
    使用Brick\Money\ExchangeProvider\ConfigurableProvider;
    使用Brick\Money\ExchangeRateProvider\BaseCurrencyProvider;
    使用砖\钱\钱;
    $provider=新的ConfigurableProvider();
    $provider->setExchangeRate('USD','CAD',1.28024);
    $provider->setExchangeRate('USD','EUR',0.838313);
    //这就是魔法发生的地方!
    $provider=新的BaseCurrencyProvider($provider,'USD');
    $converter=新的CurrencyConverter($provider);
    $money=money::of(10,'欧元');
    $converter->convert($money,'CAD',RoundingMode::DOWN);//CAD 15.27
    
    BaseCurrencyProvider
    就是为了这个目的而设计的,当您有一个与单一货币相关的汇率列表,并且希望在列表中的两种任意货币之间进行转换时


    请注意,在现实世界的应用程序中,您可能会直接使用从数据库加载汇率,而不是上面使用的
    ConfigurableProvider

    Gary Thomas已经提到过,CurrencyLayer API有一个源货币切换参数,允许您将基础货币从
    USD
    切换到
    $currency1
    参数设置为的任何货币

    但是,据我所知,您希望能够定期查询CurrencyLayer API,仅使用
    USD
    作为源货币,并自己执行汇率计算

    要实现这一点,您需要转换:

  • 货币1
    美元
  • 美元
    货币2
  • 将其转换为以下代码:

    函数convertCurrency($currency1,$currency2,$value)
    {
    $baseCurrency='USD';
    $quotes=[
    “USDCAD”=>1.28024,
    “USDEUR”=>0.838313,
    // ...
    ];
    $quote1=$quotes[$baseCurrency.$currency1];
    $quote2=$quotes[$baseCurrency.$currency2];
    返回$value/$quote1*$quote2;
    }
    兑换货币('EUR','CAD',10);//15.271622890257
    
    您还可以使用货币库,例如,为您处理这些计算(以及更多计算):

    使用Brick\Math\RoundingMode;
    使用Brick\Money\CurrencyConverter;
    使用Brick\Money\ExchangeProvider\ConfigurableProvider;
    使用Brick\Money\ExchangeRateProvider\BaseCurrencyProvider;
    使用砖\钱\钱;
    $provider=新的ConfigurableProvider();
    $provider->setExchangeRate('USD','CAD',1.28024);
    $provider->setExchangeRate('USD','EUR',0.838313);
    //这就是魔法发生的地方!
    $provider=新的BaseCurrencyProvider($provider,'USD');
    $converter=新的CurrencyConverter($provider);
    $money=money::of(10,'欧元');
    $converter->convert($money,'CAD',RoundingMode::DOWN);//CAD 15.27
    
    BaseCurrencyProvider
    就是为了这个目的而设计的,当您有一个与单一货币相关的汇率列表,并且希望在列表中的两种任意货币之间进行转换时


    请注意,在实际应用程序中,您可能会使用直接从数据库加载汇率,而不是上面使用的
    ConfigurableProvider

    在数据库中获取关键字为$currency1.$currency2的报价。然后返回$quote*$VALUE为什么不试试货币转换器API呢。示例查询:获取数据库中键为$currency1.$currency2的报价。然后返回$quote*$VALUE为什么不试试货币转换器API呢。示例查询:谢谢兄弟!但是,如果我想使用提供的信息将例如EUR转换为BRL,考虑到来源是USD?@fabionvs,请查看REST API文档。有一个标题是“源代码C”