Free PHP Currency conversion APIs with Examples

If you are performing currency conversion on your project then it is important to stay up-to-date on the latest currency rates.

This is not possible with only using database.

There are many available free and paid APIs for currency conversion.

In this tutorial, I show you some free APIs for currency conversion that you can use it in your PHP project.

Free PHP Currency conversion APIs with Examples


Contents

  1. Exchangerate.host
  2. Free Currency Converter API
  3. Abstract API
  4. 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 here.

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 also requires an API key.
  • Navigate to the following link to get the API key.
  • At the time of writing this API allow 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. Conclusion

Only Free Currency Converter API updates rates in less time compared to the other two.

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