Ad Code

Build Currency Converter in PHP with Google API

 <?php
function convertCurrency($currency_from,$currency_to,$convert_amount) {  
    $currency_from = urlencode($currency_from);
    $currency_to = urlencode($currency_to);  
    $currencyUrl = "https://www.google.com/search?q=".$currency_from."+to+".$currency_to;
    $currencyDetails = file_get_contents($currencyUrl);
    $currencyData = preg_split('/\D\s(.*?)\s=\s/',$currencyDetails);
    $conversion_rate = (float) substr($currencyData[1],0,7);  
    $total_converted_currency_amount = $convert_amount*$conversion_rate;
    $currencyJsonData = array( 'rate' => $conversion_rate, 'total_converted_currency_amount' =>$total_converted_currency_amount, 'currency_from' => strtoupper($currency_from), 'currency_to' => strtoupper($currency_to));
    $aa = json_encode( $currencyJsonData );
    return json_decode($aa, true);
}

$g = convertCurrency('USD','INR',1);
echo "<pre>";
print_r($g);
?>

Array
(
    [rate] => 75.1324
    [total_converted_currency_amount] => 75.1324
    [currency_from] => USD
    [currency_to] => INR
)

Post a Comment

0 Comments