reenadak

convert currency

Feb 20th, 2018
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.91 KB | None | 0 0
  1.     /*
  2.     - Currency Conversion (currencyconverterapi)
  3.     - Converts one currency to another
  4.    
  5.     $from     - The 3-letter ISO currency to convert from (ie. USD)
  6.     $to       - The 3-letter ISO currency to convert to (ie. GBP)
  7.     $amount   - The amount of $from to convert to $to (ie. 1000)
  8.     $timeout  - Number of seconds to wait before giving up
  9.     */
  10.    
  11.     public function convertCurrency($from, $to, $amount, $timeout = 5) {
  12.         $ch = curl_init();
  13.        
  14.         curl_setopt($ch, CURLOPT_URL, "http://free.currencyconverterapi.com/api/v3/convert?q=" . $from . "_" . $to . "&compact=y");
  15.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  16.         curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  17.        
  18.         $data = curl_exec($ch);
  19.         curl_close($ch);
  20.        
  21.         $arr = json_decode($data, true);
  22.        
  23.         return $arr[$from . "_" . $to]["val"] * $amount;
  24.     }
Advertisement
Add Comment
Please, Sign In to add comment