Skip to content

Instantly share code, notes, and snippets.

@BAHC
Last active June 28, 2018 19:54
Show Gist options
  • Save BAHC/8e34ca1556f644ee5e1248bfeea4a38a to your computer and use it in GitHub Desktop.
Save BAHC/8e34ca1556f644ee5e1248bfeea4a38a to your computer and use it in GitHub Desktop.
Exchange Rates
<?php
function exchange_rate($_opt = [])
{
$_pairs = [];
foreach($_opt as $_k=>$_v)
{
$_pairs[] = $_k.'_'.$_v;
}
if(!count($_pairs)) return null;
$_pair = implode(',', $_pairs);
$_url = 'http://free.currencyconverterapi.com/api/v5/convert?q='.$_pair.'&compact=y';
$a_json = json_decode( file_get_contents($_url), true);
$_res = [];
foreach($a_json as $_k => $_v)
{
$_res[$_k] = number_format($_v['val'], 2, '.', '');
}
return ($_res);
}
//Examples:
print_r( exchange_rate(['EUR'=>'USD','USD'=>'EUR']) );
print_r( exchange_rate(['EUR'=>'RUB']) );
print_r( exchange_rate(['EUR'=>'TRY']) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment