Create currency converter in PHP

In this article, we will be creating a simple currency converter in PHP. You just need to tell the source and target currency and the amount. You can check the demo as well.

Demo

We will be using a FREE api to convert any currency to anyother at the standard market rates. Market rates may differ on time, also it may take some hours for the API to update the rates if the currency goes up or down.

You just have to provide both currencies and the amount you want to convert, and it will be converted accordingly.

No external library has been used in this script.

We start off by creating a simple form to get the source and destination currency, and the amount he wants to convert. For example, if you want to convert 143 USD dollars to PKR:

  • Source currency = USD
  • Destination currency = PKR
  • Amount = 143

Simple !

We will be creating just 2 files:

  1. index.php to get user input.
  2. convert.php to do the conversion.
<form method="POST" action="convert.php">
    <input type="text" name="from" placeholder="From" /> <br />
    <input type="text" name="to" placeholder="To" /> <br />
    <input type="number" name="amount" placeholder="Amount" /> <br />
    <input type="submit" value="Convert" /> <br />
</form>

convert.php

In this file, first we have to get the user input and save it in separate variable:

<?php
    $from = $_POST["from"];
    $to = $_POST["to"];
    $amount = $_POST["amount"];
?>

Then, we have to call the PHP built-in curl function and pass these variables as parameters to do the conversion. The URL of API is:

https://free.currconv.com/api/v7/convert?q=USD_PKR&compact=ultra&apiKey={your_api_key}

Get API key

You just have to change the currency according to yours (from the variables) and it will return the standard rate of 1 unit. For example, right now, the above URL will return 135. It is the value of 1 USD in PKR currency.

$string = $from . "_" . $to;
$curl = curl_init();
curl_setopt_array($curl, array(
    CURLOPT_URL => "https://free.currconv.com/api/v7/convert?q=" . $string . "&compact=ultra&apiKey={your_api_key}",
    CURLOPT_RETURNTRANSFER => 1
));
  • CURLOPT_RETURNTRANSFER this will return the output as string in curl_exec() function instead of outputting it directly.
  • $string variable is created separately because it will be re-used in the next step.

Execute the curl command:

$response = curl_exec($curl);
print_r($response);

At this point, when you fill the form and submit you will see something similar to the output:

{“USD_PKR”:141.590271}

This is the response in JSON format. You have to convert it to PHP objects and array format:

$response = curl_exec($curl);
$response = json_decode($response, true);
print_r($response);
  • 2nd parameter to json_encode() function is true, which make sure the string will be converted in PHP array.

Now, it will print something like this:

Array ( [USD_PKR] => 141.590271 )

This is the conversion rate of currency of 1 unit. You can get it’s value in a variable:

$rate = $response[$string];

And can find the total by multipying this with the number of dollars user entered:

$total = $rate * $amount;
print_r($total);

Now, you will be able to view the actual price after conversion. So your currency converter in PHP is ready. If you have any problem in following this, kindly do let us know.

[wpdm_package id=’114′]