Free PHP Currency conversion APIs with Examples

For websites and applications that deal with international transactions or multi-currency payments, currency conversion is an essential feature. Manually updating or using the database to manage exchange rates can be time-consuming and inaccurate.

Using a currency conversion API can provide accurate and up-to-date exchange rates for multiple currencies in real time.

In this tutorial, I will discuss some of the best free PHP currency conversion APIs available and provide examples of how to use them in your PHP projects. These APIs can speed up the process of converting currencies, save you time, and improve the user experience for your clients.

Free PHP Currency conversion APIs with Examples


Contents

  1. Exchangerate.host
  2. Free Currency Converter API
  3. Abstract API
  4. CurrencyAPI
  5. Conclusion

1. Exchangerate.host

  • For using this API you do not need to register on the website to get the API key.
  • Conversion rates are updated on 1 day basis.
  • You can convert a currency from a single request.
  • You can learn more about this API from official document.

Request –

Send request to https://exchange-rates.abstractapi.com/v1/convert/. With URL pass 3 parameters –

  • base – Currency symbol that needs to convert.
  • amount – Conversion amount
  • symbols – Targeting currency symbol.

Decode the JSON response and check the success value. If it is true then read the conversion amount.

Example

<?php

function convertCurrency($from_currency="USD",$to_currency="INR",$amount=1){

    $req_url = 'https://api.exchangerate.host/latest?base='.$from_currency.'&amount='.$amount.'&symbols='.$to_currency;

    $response_json = file_get_contents($req_url);

    $hasConversion = false;
    $converted_amount = 0;
    if(false !== $response_json) {
        try {
            $response = json_decode($response_json);

            if($response->success === true) {

                 // Read conversion rate
                 $converted_amount = round($response->rates->$to_currency,2);

                 $hasConversion = true;
            }

        } catch(Exception $e) {
            // Handle JSON parse error...

        }
    }

    $return_arr = array(
        "success" => $hasConversion,
        "amount" => $amount,
        "converted_amount" => $converted_amount
    );

    return $return_arr;
}

$from_currency = "USD";
$to_currency = "INR";
$amount = 10;

$response = convertCurrency($from_currency,$to_currency,$amount);

echo "<pre>";
print_r($response);
echo "</pre>";

2. Free Currency Converter API

Get API Key –

  • For using this API you have to register and get the API key.
  • Navigate to the following link to register.
  • They mail you a free API key in 3-5 business days.
  • You can send 100 request per hour and currency rates are updated on 1 hours basis.
  • API expires in 1 month. You get renewal mail when your API key is expired.

Request –

Send a request to https://free.currconv.com/api/v7/convert. Here, pass 3 parameters –

  • q – Pass from and to currency symbols separated by an underscore (_) e.g. USD_INR.
  • compact – Pass ultra.
  • apiKey – Your API key.

It returns a JSON response that has a conversion rate. Multiply this with the amount to get the conversion amount.

Example

<?php

function convertCurrency($from_currency="USD",$to_currency="INR",$amount=1){

     $API_KEY = '366fe789becfb4cf42e9';

     $from_Currency = urlencode($from_currency);
     $to_Currency = urlencode($to_currency);
     $query = "{$from_Currency}_{$to_Currency}";

     $json = file_get_contents("https://free.currconv.com/api/v7/convert?q={$query}&compact=ultra&apiKey={$API_KEY}");
     $obj = json_decode($json, true);

     $hasConversion = false; 
     $converted_amount = 0;

     if(isset($obj["$query"])){
          $hasConversion = true;

          $val = floatval($obj["$query"]);
          $total = $val * $amount;

          $converted_amount = number_format($total, 2, '.', '');
     }

     $return_arr = array(
          "success" => $hasConversion,
          "amount" => $amount,
          "converted_amount" => $converted_amount
     );

     return $return_arr;

}

$from_currency = "USD";
$to_currency = "INR";
$amount = 10;

$response = convertCurrency($from_currency,$to_currency,$amount);

echo "<pre>";
print_r($response);
echo "</pre>";

3. Abstract API

Get API key –

  • This API requires an API key.
  • Signup to Abstract API to get the API key.
  • At the time of writing this API allows 500 calls per month.
  • It has also provided a dashboard from where you can view request usage.

Request –

Send a request to https://exchange-rates.abstractapi.com/v1/convert/. Here, pass 4 parameters –

  • api_key – Your API key.
  • base – Currency symbol that needs to convert.
  • target – Targeting currency symbol.
  • base_amount – Conversion amount.

It returns a JSON response. Decode the response and read the values. It also returns the conversion rate with the response.

Example

<?php

function convertCurrency($from_currency="USD",$to_currency="INR",$amount=1){

     $API_KEY = "563eef59fb3c4742bag8c0d9d298487f";

     // Initialize cURL.
     $ch = curl_init();
  
     // Set the URL that you want to GET by using the CURLOPT_URL option.
     curl_setopt($ch, CURLOPT_URL, 'https://exchange-rates.abstractapi.com/v1/convert/?api_key='.$API_KEY.'&base='.$from_currency.'&target='.$to_currency.'&base_amount='.$amount);

     // Set CURLOPT_RETURNTRANSFER so that the content is returned as a variable.
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

     // Set CURLOPT_FOLLOWLOCATION to true to follow redirects.
     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

     // Execute the request.
     $response_json = curl_exec($ch);

     $response = json_decode($response_json);

     // Close the cURL handle.
     curl_close($ch);

     $hasConversion = false;
     $converted_amount = 0;
     $exchange_rate = 0;

     if(isset($response->converted_amount)){
          $hasConversion = true;
          $converted_amount = $response->converted_amount;
          $exchange_rate = $response->exchange_rate;
     }

     $return_arr = array(
          "success" => $hasConversion,
          "amount" => $amount,
          "converted_amount" => $converted_amount,
          "exchange_rate" => $exchange_rate
     );

     return $return_arr;

}

$from_currency = "USD";
$to_currency = "INR";
$amount = 10;

$response = convertCurrency($from_currency,$to_currency,$amount);

echo "<pre>";
print_r($response);
echo "</pre>";

4. CurrencyAPI

  • This API requires an API key.
  • Register your account under https://currencyapi.com
  • Navigate to the following link to get the API key: https://app.currencyapi.com/api-keys
  • At the time of writing this API allows 300 calls per month. Paid plans are available which will give you much more up-to-date rates and high volume.
  • You can view request usage and your most recent requests from the dashboard, which has also been provided.

Request 1 –

The only API you can use in the free plan is https://api.currencyapi.com/v3/latest, which returns conversion rates.

It returns USD exchange rates by default, but you can change that by specifying base_currency parameter and to return the conversion rate for a specific currency then specify it using the currencies parameter.

For getting the conversion rate you need to multiply your amount with the returned exchange rate.

Example

<?php

function convertCurrency($from_currency="USD",$to_currency = "INR", $amount = 1) {

     $API_KEY = "RypupO4Kxh2Ck4VRHS7kw9JDWkUfi5i8dUqK0Jmgx7s";

     // Initialize cURL.
     $ch = curl_init();

     // Set the URL that you want to GET by using the CURLOPT_URL option.
     curl_setopt($ch, CURLOPT_URL, 'https://api.currencyapi.com/v3/latest?apikey='.$API_KEY."&base_currency=".$from_currency."&currencies=".$to_currency);

     // Set CURLOPT_RETURNTRANSFER so that the content is returned as a variable.
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

     // Set CURLOPT_FOLLOWLOCATION to true to follow redirects.
     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

     // Execute the request.
     $response_json = curl_exec($ch);

     $response = json_decode($response_json);

     // Close the cURL handle.
     curl_close($ch);

     $hasConversion = false;
     $converted_amount = 0;
     $exchange_rate = 0;

     if (isset($response->data->{$to_currency})) {
          $hasConversion = true;
          $exchange_rate = $response->data->{$to_currency}->value;
          $converted_amount = $amount * $exchange_rate; 
     }

     $return_arr = array(
          "success" => $hasConversion,
          "amount" => $amount,
          "converted_amount" => $converted_amount,
          "exchange_rate" => $exchange_rate
     );

     return $return_arr;

}

$from_currency = "USD";
$to_currency = "INR"; 
$amount = 10;

$response = convertCurrency($from_currency,$to_currency,$amount);

echo "<pre>"; 
print_r($response); 
echo "</pre>";

Request 2 –

You need to upgrade to the paid plan if you want direct amount conversion to any currency.

Send a request to https://api.currencyapi.com/v3/convert.

You must pass these four parameters:

  • apikey – Your API key.
  • base_currency – Currency symbol that needs to convert.
  • currencies – Targeting currency symbol. (This is optional but will result in a much smaller response)
  • Value – Conversion amount.
  • Optional you could also pass a date (date=2023-04-01) to calculate the conversion with the end-of-day value for this currency and day.

The API returns a JSON response. Decode the response and read the values.

Example

<?php

function convertCurrency($from_currency = "USD", $to_currency = "INR", $amount = 1) {

     $API_KEY = "RypupO4Kxh2Ck4VRHS7kw9JDWkUfi5i8dUqK0Jmgx7s";

     // Initialize cURL.
     $ch = curl_init();

     // Set the URL that you want to GET by using the CURLOPT_URL option.
     curl_setopt($ch, CURLOPT_URL, 'https://api.currencyapi.com/v3/convert?apikey=' . $API_KEY . '&base_currency=' . $from_currency . '&currencies=' . $to_currency . '&value=' . $amount);

     // Set CURLOPT_RETURNTRANSFER so that the content is returned as a variable.
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

     // Set CURLOPT_FOLLOWLOCATION to true to follow redirects.
     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

     // Execute the request.
     $response_json = curl_exec($ch);

     $response = json_decode($response_json);

     // Close the cURL handle.
     curl_close($ch);

     $hasConversion = false;
     $converted_amount = 0;
     $exchange_rate = 0;

     if (isset($response->data->{$to_currency})) {
           $hasConversion = true;
           $converted_amount = $response->data->{$to_currency}->value;
     }

     $return_arr = array(
          "success" => $hasConversion,
          "amount" => $amount,
          "converted_amount" => $converted_amount
     );

     return $return_arr;

}

$from_currency = "USD"; 
$to_currency = "INR"; 
$amount = 10; 

$response = convertCurrency($from_currency,$to_currency,$amount); 

echo "<pre>"; 
print_r($response); 
echo "</pre>";

5. Conclusion

Only Free Currency Converter API updates rates in less time compared to the other 3 APIs free plan.

To minimize unnecessary calls use Database, COOKIE, or SESSION to store currency conversion rates and update their value on regular time intervals by sending an API request.

You can go for paid plans if you want more API requests and get the latest conversion rate in less time.

If you found this tutorial helpful then don't forget to share.

Leave a Comment