Free Currency Converter PHP using Fixer io API

By Saruque Ahamed Mollick

Hello, Today I am gonna provide you Currency Converter API  PHP Source Code with explanation and how to use that PHP Script.

This tutorial will help you to create your own PHP Currency Converter Script using Fixer.io API.



Features of this PHP Script :

  1. You can convert one currency into another.
  2. You will have all the currency in a  list as a drop-down menu. ( All the drop-down options are being fetched from the API JSON Data)
  3. You will get the conversion result of currency exchange as well as the Reverse currency exchange result.
  4. Easy to use and easy to understand.
  5. Optimized Code and Light Weight.

You can see the live demo of this PHP Script from Here

View Live Demo Of Currency Converter PHP Script
 

 
You Can Download the PHP Currency Conversion Script From here

GitHub Link

Direct Download Link

How to create free currency converter PHP Script Using Fixer API

At first, go to https://fixer.io
Create an account there to get The API key.
For now, you will get 1000 SMS/ month for your free account. If you want to increase the number of SMS you can choose their premium plans.
But I will suggest you create a free account first to test your script.

After creating an account you will get a unique API key.

Now here your Coding Starts.
At first, you need to create a PHP file to fetch all the data from Fixer JSON file.

The JSON file URL looks like this,

http://data.fixer.io/api/latest?access_key=API_KEY_HERE&format=1

And the JSON File looks like this,

{
  "success":true,
  "timestamp":1534049006,
  "base":"EUR",
  "date":"2018-08-12",
  "rates":{
    "AED":4.195718,
    "AFN":82.71609,
    "ALL":125.647934,
    "AMD":550.747778,
    "ANG":2.107342,
    "AOA":296.657252,
    "ARS":33.354893,
    "AUD":1.564088,
    "AWG":2.047769,
    "AZN":1.944686,
    "BAM":1.948851,
    "BBD":2.286099,
    "BDT":96.512186,
    "BGN":1.957936,
    "BHD":0.431017,
    "BIF":2040.058712,
    ----------
    ----------  //more data
    ----------
    ----------
}
}

If you have the API key then you can get this JSON file URL easily by accessing that URL.
The URL will also be provided in Fixer account.

You might also be interested in,

Find Out What Happened On This Day PHP Script Using Hiztory API

Movie Video Trailers And Info PHP Script That Uses TMDB API

Get the distance between two places in PHP with Google Map API

Create a PHP file to fetch all the currency rate and data from JSON File

This is your PHP file to fetch all the currency data

<?php


$api="your api key here";   ///// Provide your fixer.io api key here


$string = file_get_contents("http://data.fixer.io/api/latest?access_key=$api&format=1");
  $json = json_decode($string, true);
  
  
  $i=0;
  foreach ($json['rates'] as $key => $value) {
    $currency[$i]=$key;
    $rate[$i]=$value;
    $i=$i+1;
        
  }
  
?>
$api="your api key here";

Put your API key of fixer here.

 $string = file_get_contents("http://data.fixer.io/api/latest?access_key=$api&format=1");

Thereafter, You have to use file_get_contents function to read the contents of the JSON file into a string.

Then, json_decode is used to convert the data into an object.

So now you carefully look at the JSON file structure. We have currency names and their value. SO we can use those as array.

 foreach ($json['rates'] as $key => $value)

 { 

$currency[$i]=$key; 

$rate[$i]=$value; 

$i=$i+1; }

By this foreach loop, we can easily store all the currency names and their value in array variable.

$currency[] array is used to store the currency names and $rate[] array is used to store their rates. (our base currency is EUR here)

As we have used array so it is cleared that $currency[5] will be the currency name of 6th currency listed in our JSON file. (as index starts with zero)

PHP Weather Forecast Script That Uses OpenWeatherMap API

Now come to our main index.php file

PHP code of currency conversion

<?php
include('fetch.php');

?>
<!DOCTYPE html>
<html>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style type="text/css">
  body{
    background-color: lavender;
    text-align: center;
    margin-top: 120px;
  }
  footer {
    font-size: 12px;
    margin: 0 auto;
    max-width: 1200px;
    position: relative;
    width: 95%;
}

}
</style>
<head>
  <title>Currency Converter</title>
</head>
<body>
  <h1>Currency Converter Using Fixer.io API</h1>
  
  
  <form method="post">
    Enter Amount:
    <input type="text" name="amount" value="1"><br><br>
    From:
    <select name="from">
      <?php
        for($i=0;$i<count($rate);$i++){
          echo "<option value=\"$i\">$currency[$i]</option>";
        }


      ?>
      
      </select> 
      TO: <select name="to">
      <?php
        for($i=0;$i<count($rate);$i++){
          echo "<option value=\"$i\">$currency[$i]</option>";
        }


      ?>
    </select><br><br>
    <input type="submit" name="submit" value="Convert!">
  </form>
  <br>
  <?php
  if (isset($_POST['amount'])) {
    $amount=$_POST['amount'];
    $from=$_POST['from'];
    $to=$_POST['to'];
    $combine= array_combine($currency, $rate);
    $from_currency=$rate[$from];
    $to_currency=$rate[$to];
    $result=$to_currency/$from_currency;
    $resultrev=$from_currency/$to_currency;
  $output=$result*$amount;
  $reverse=$resultrev*$amount;
  echo " Conversion Result: <br><br> $amount $currency[$from] = "."$output $currency[$to]";

  echo "<br><br>Reverse Result:<br><br>$amount $currency[$to] = "."$reverse $currency[$from]";

  
  }

  ?>
  <br>
  <br><br><br>
  
</body>


</html>

 

Leave a Reply

Your email address will not be published. Required fields are marked *