替代更换api.exchangerate.host的货币汇率接口

api.exchangerate.host 的API不能白嫖了, 找了个新的汇率接口api.vatcomply.com

原有代码

function exchange($from, $to)
{
    try {
        $result = file_get_contents('https://api.exchangerate.host/latest?symbols=' . $to . '&base=' . $from);
        $result = json_decode($result, true);
        return $result['rates'][$to];
    } catch (Exception $e){
        echo "Exchange error: ".$e;
        return "Exchange error: ".$e;
    }

}

更新后

function exchange($from, $to)
{
    try {
        $result = file_get_contents('https://api.vatcomply.com/rates?base='. $from);
        $result = json_decode($result, true);
        return $result['rates'][$to];
    } catch (Exception $e){
        echo "Exchange error: ".$e;
        return "Exchange error: ".$e;
    }

}

添加新评论 »