How to get the Currency Conversion Rate in php [duplicate]

How to get the Currency Conversion Rate in php?

Here, the function is used to get the currency conversion rate,

Get the  currency conversion rate like this:
$rate = $this->currencyRate(‘USD’, ‘INR’);
// To get the Currency Rate from Third Party API

public function currencyRate($fromCurrency, $toCurrency)
{
    $conversion_rate = 0;

    if (!empty($fromCurrency) && !empty($toCurrency) )
    {

       $currencyString = strtoupper($fromCurrency.'_'.$toCurrency);  

       $fetchUrl = 'http://free.currencyconverterapi.com/api/v5/convert?q='.$currencyString.'&compact=y';

 
       try 
       {

          $handle_contents =  file_get_contents($fetchUrl);

          $contentsDecoded= @json_decode($handle_contents, TRUE);

          if(!empty($contentsDecoded[$currencyString])){

             $curData = $contentsDecoded[$currencyString];

             if(!empty($curData['val'])){

                $conversion_rate = floatval($curData['val']);
             }

          }
       } 
       catch (Exception $e) 
       {
       }
   }

    return($conversion_rate);

}

// function Ends

http://www.stackofcodes.in/2018/09/16/how-to-get-the-currency-conversion-rate-in-php/

To view, the Main Post visit the link:

Leave a Comment