In this tutorial I am going to develop a currency converter with the help of Google Finance Currency API. You can use below currency converter function in your project to get updated currency values and convert into other countries value.

There are lots of API available to convert currency, but here i am using Google Finance Currency Converter api . There are basically 3 parameter required: From Currency, To Currency and Amount. Using these three parameter i will send HTTP request on “Google Finance Currency Converter” using file_get_contents and in response you’ll get raw data in HTML format. In the final stage you need to parse html data and get the converted amount.
Here is the function.

function calculateCurrency($fromCurrency, $toCurrency, $amount) {
    $amount = urlencode($amount);
    $fromCurrency = urlencode($fromCurrency);
    $toCurrency = urlencode($toCurrency);
    $rawdata = file_get_contents("$amount&from=$fromCurrency&to=$toCurrency");
      $data = explode('bld>', $rawdata);
      $data = explode($toCurrency, $data[1]);
      return round($data[0], 2);

Call above function with given parameters

echo calculateCurrency('USD','INR',1);

I have created online currency converter demo using above function.



If you like this post please don’t forget to subscribe my public notebook for more useful stuff