How to use our PHP Currency Converter
Converting currencies using our PHP Exchange Rate API is as easy as posting a HTTP request. Your requests would need to follow
this simple pattern:
Here are some examples in PHP:
1. Simple Currency Conversion (in PHP):
To convert from 12.50 USD (US Dollar) to GBP (British Pound):
$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):
$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:
$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: $)
3. Automatic Country/Flag Information (in PHP):
To retrieve your user's national flag and other country information:
$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
How Our API Works
Python
PHP
Ruby
Perl
ASP.NET
Java
Applescript
iPhone C