reenadak icon

convert currency

reenadak | PRO | 02/20/18 06:22:49 PM UTC | 0 ⭐ | 345 👁️ | Never ⏰ | []
PHP |

910 B

|

None

|

0 👍

/

0 👎

    /*
    - Currency Conversion (currencyconverterapi)
    - Converts one currency to another
   
    $from     - The 3-letter ISO currency to convert from (ie. USD)
    $to       - The 3-letter ISO currency to convert to (ie. GBP)
    $amount   - The amount of $from to convert to $to (ie. 1000)
    $timeout  - Number of seconds to wait before giving up
    */
   
    public function convertCurrency($from, $to, $amount, $timeout = 5) {
        $ch = curl_init();
       
        curl_setopt($ch, CURLOPT_URL, "http://free.currencyconverterapi.com/api/v3/convert?q=" . $from . "_" . $to . "&compact=y");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
       
        $data = curl_exec($ch);
        curl_close($ch);
       
        $arr = json_decode($data, true);
       
        return $arr[$from . "_" . $to]["val"] * $amount;
    }

Comments