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:

How to get the Currency Conversion Rate in php

How to get the Currency Conversion Rate in php?

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

To get the currency conversion rate , just call the function with the parameters.
Like:
$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

To get the currency conversion rate , just call the function with the parameters.
Like:
$rate = $this->CurrencyRate('USD', 'INR');

How to get the Currency Rate from Third Party API

How to get the Currency Rate from Third Party API?

This Small  function is used to call the API & get the currency Conversion Rate.

By using this  you can get easily the conversion rate.

$fromCurrency = "USD";
$toCurrency   = "INR";


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) {
        $conversion_rate = 0;
   }

}


# Now, Show the Result::

print_r($conversion_rate);