How to use our PHP Currency Converter

Before you start using the ExchangeRate API you will need a key, which you can get here.

Once you've done that, converting currencies using our PHP Exchange Rate API is as simple as posting a few HTTP requests. Here are some examples:

1. Simple Currency Conversion (in PHP):

To convert from 12.50 USD (US Dollar) to GBP (British Pound):

$from = "USD";
$to = "GBP";
$amount = 12.50;
$url = "http://www.exchangerate-api.com/".$from."/".$to."/".$amount."?k=API_KEY";
$result = file_get_contents($url);
echo $result;

Or to get how many Japanese Yen (JPY) there are to the South African Rand (ZAR):

$url = "http://www.exchangerate-api.com/zar/jpy/?k=API_KEY";
$result = file_get_contents($url);
echo $result;

Error Codes:

The API returns the following error codes:
  -1 if an invalid amount is used (must be numeric floating point or integer numbers)
  -2 if either of the 3 letter currency codes are invalid (Check our supported currencies)
  -3 if an invalid key is used (Get an API key)
  -4 if your monthly API query limit is reached (Click here for extended usage)
  -5 if an unresolvable IP address is provided (This only applies for our "Automatic Country/Flag Information" query.)



We also offer two additional types of query:




2. Automatic Currency Detection/Conversion (in PHP):

To convert from 12.50 USD (US Dollar) to your user's currency:

$ip = $_SERVER['REMOTE_ADDR'];

$url = "http://www.exchangerate-api.com/auto/USD/".$ip."/12.50?k=API_KEY";
$result = file_get_contents($url);
$array = explode("|", $result);
$converted_amount = $array[0];
$symbol_unicode = $array[1];
$currency_code = $array[2];

echo($converted_amount); //FINAL CONVERTED AMOUNT
echo($currency_code); //FINAL 3-DIGIT CURRENCY CODE (EG: USD)

//If you want to display the currency's symbol in HTML:
$symbol_html = "";
$symbol_characters = explode(",",$symbol_unicode);
for($i=0;$i<count($symbol_characters);$i++) {
    $symbol_html .= "&#".$symbol_characters[$i].";";
    }
echo($symbol_html); //FINAL SYMBOL HTML (EG: $)

Free 14-Day Trial

Sign Up Now!

Get started right away!



3. Automatic Country/Flag Information (in PHP):

To retrieve your user's national flag and other country information:

$ip = $_SERVER['REMOTE_ADDR'];

$url = "http://www.exchangerate-api.com/country_info/".$ip."?k=API_KEY";
$result = file_get_contents($url);
$array = explode("|", $result);
$country_name = $array[0];
$country_code_2 = $array[1];
$country_code_3 = $array[2];
$currency_code = $array[3];
$symbol_unicode = $array[4];
$flag_url = $array[5];

echo($country_name); //FINAL COUNTRY NAME
echo($country_code_2); //FINAL 2-DIGIT COUNTRY CODE (EG: US)
echo($country_code_3); //FINAL 3-DIGIT COUNTRY CODE (EG: USA)
echo($currency_code); //FINAL 3-DIGIT CURRENCY CODE (EG: USD)

//If you want to display the currency's symbol in HTML:
$symbol_html = "";
$symbol_characters = explode(",",$symbol_unicode);
for($i=0;$i<count($symbol_characters);$i++) {
    $symbol_html .= "&#".$symbol_characters[$i].";";
    }
echo($symbol_html); //FINAL SYMBOL HTML (EG: $)

//If you want to display an image of the flag in HTML:
$flag_image = "<img src='".$flag_url."' />";
echo($flag_image); //FINAL FLAG IMAGE

Free 14-Day Trial

Sign Up Now!

Get started right away!