실시간 환율 받아오기 (Get real-time currency information)

Code snippet to retrieve real-time currency from exchangeratesapi.io

<?php 
  echo get_currency("AUD", "KRW"); 
  /* 
  * Retrieve real-time currency from exchangeratesapi.io
  * 
  * @param string $from_currency e.g.) AUD 
  * @param string $to_currency e.g.) KRW 
  * @access public 
  
  * @return float currency value
  * 
  */ 
  function get_currency($from_currency, $to_currency) { 
    $url = "https://api.exchangeratesapi.io/latest?base=$from_currency&symbols=$from_currency,$to_currency"; 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    $currency_value = curl_exec($ch); 
    curl_close ($ch); 
    
    return json_decode($currency_value)->rates->$to_currency; 
  }

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.