Currency Conversion using PHP

Viewed 84610

I'm looking for a way to convert any amount from one currency to another on a website. The user would enter something like '100' and select USD as the currency, and then chooses Australian or Canadian dollars as the currency to convert to. When he clicks the 'Convert' button, I'd like to convert that amount automatically, through some API, and show him the amount in the currency he chose to convert to.

Any ideas?

15 Answers

After searching a lot, found this.

// Fetching JSON
$req_url = 'https://api.exchangerate-api.com/v4/latest/USD';
$response_json = file_get_contents($req_url);

// Continuing if we got a result
if(false !== $response_json) {

    // Try/catch for json_decode operation
    try {

    // Decoding
    $response_object = json_decode($response_json);

    // YOUR APPLICATION CODE HERE, e.g.
    $base_price = 12; // Your price in USD
    $EUR_price = round(($base_price * $response_object->rates->EUR), 2);

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

This is working fine. The snippet is from: https://www.exchangerate-api.com/docs/php-currency-api

Use Given code for currency Converter PHP

public function convertCurrency($from, $to, $amount)    
{
    $url = file_get_contents('https://free.currencyconverterapi.com/api/v5/convert?q=' . $from . '_' . $to . '&compact=ultra');
    $json = json_decode($url, true);
    $rate = implode(" ",$json);
    $total = $rate * $amount;
    $rounded = round($total);
    return $total;
}

You can also check: https://free.currencyconverterapi.com

Disclaimer, I'm the author of this website.

A sample conversion URL is: http://free.currencyconverterapi.com/api/v6/convert?q=PHP_EUR,EUR_PHP&compact=ultra&apiKey=sample-api-key which will return a value in json format, e.g. {"PHP_EUR":0.016434,"EUR_PHP":60.849184}

You should note about the limitations though like maximum requests and queries (details found the website). I've implemented limits because there have been abusers of the service.

I've started this since 2014 and has been up ever since (aside from the link change and maintenance down times along the way). I personally use it for my other websites, and provided the service publicly so it can help other devs as well.

Anyway hope this helps, and here's a sample PHP code:

<?php
function convertCurrency($amount, $from, $to){
  $conv_id = "{$from}_{$to}";
  $string = file_get_contents("https://free.currencyconverterapi.com/api/v6/convert?q=$conv_id&compact=ultra&apiKey=sample-api-key");
  $json_a = json_decode($string, true);

  return $amount * round($json_a[$conv_id], 4);
}

echo(convertCurrency(5, "USD", "PHP"));
?> 

Please check below currency change example.

    function oneUSDTo($amount, $toCurrency)
{
$currencyUrl = $amount."usd+to+$toCurrency";
$url = "https://www.google.com/search?q=".$currencyUrl;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$result = curl_exec($ch);
$data = explode("$amount US Dollar = ",$result);
return (float) substr($data[1],0,10);
}

//Usage: oneUSDTo("type currency code here");
echo oneUSDTo("5","EUR");
https://fincharts.info/charts/exchange/?key=api_key&&from=USD&to=GBP,BWP,ZAR&amount=45854&date=2018-03-03&base=USD

seems you have to signup for an api key, I think

<?php
//  replace this key from  fixer.io after getting  a free API access key:

$API = '314259bbe6de76b961c84a5244ac0fc0';

function convertCurrency($API, $amount, $from = 'EUR', $to = 'USD'){
  $curl = file_get_contents("http://data.fixer.io/api/latest?access_key=$API&symbols=$from,$to");

  if($curl)
  {
    $arr = json_decode($curl,true);
    if($arr['success'])
    {
        $from = $arr['rates'][$from];
        $to = $arr['rates'][$to];

        $rate = $to / $from;
        $result = round($amount * $rate, 6);
        return $result;
    }else{
        echo $arr['error']['info'];
    }
  }else{
    echo "Error reaching api";
  }
}

echo convertCurrency($API, 1, 'USD', 'EGP');
Related