In this tutorial, I will show you how to convert currency in PHP using Google Finance API. Using the API you can easily convert any currency to your desired country currency. You have to send out http request to Google finance with three values – amount, from currency and to currency code. It sends back the response as HTML which you have to parse and extract the converted amount.
PHP script to Convert Currency
Following is the currency conversion script. It uses file_get_contents() function to send http request and receives raw html response. Then it parses the html and displays the converted currency value.
<?php $amount = 100; $from_currency = urlencode('USD'); $to_currency = urlencode('INR'); $amt = urlencode($amount); $data = file_get_contents("https://finance.google.com/finance/converter?a=$amt&from=$from_currency&to=$to_currency"); $data = explode('bld>', $data); $data = explode($to_currency, $data[1]); echo round($data[0], 2); ?>
You can create a function for currency convertor with 3 parameters as amount, from currency code and to currency code. Hope this tutorial will help you to convert currency in PHP using Google Finance API.