PHP 貨幣轉換


 

使用以下代碼,可以獲取當天的匯率進行貨幣轉換

function convert_currency($number, $currency) {
    // Fetching JSON
    $req_url = 'https://api.exchangerate-api.com/v4/latest/USD';
    $response_json = file_get_contents($req_url);
    // Continuing if we got a result
    if (false !== $response_json) {
        // Try/catch for json_decode operation
        try {
            // Decoding
            $response_object = json_decode($response_json);
            if (!property_exists($response_object->rates, $currency)) {
                return 0;
            }
            return round($number / $response_object->rates->$currency, 2);
        } 
        catch (Exception $e) {
            return 0;
        }
    }
}
echo convert_currency(100, "CNY");


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM