Search code examples
phplaravelforex

exception 'Viewflex\Forex\ForexException' with message 'Error retrieving exchange rate


am using version of this package "viewflex/forex": "^0.1.1" But while updating the currency rate for -

$usdRate = $server->getRate('USD', 'INR');

Getting error as below exception 'Viewflex\Forex\ForexException' with message 'Error retrieving exchange rate.

Any Help!!! Thanks,


Solution

  • Looking into their souce code, I see this:

    https://github.com/viewflex/forex/blob/master/src/Forex.php#L57-L68

            if (
                array_key_exists('rates', $content)
                && array_key_exists($target, $content['rates'])
                && $content['rates'][$target]
            ) {
                $rate = floatval($content['rates'][$target]);
            } else {
                throw new ForexException('Error retrieving exchange rate.');
            }
            if($rate <= 0)
                throw new ForexException('Error retrieving exchange rate.');
    

    So it appears that if the response doesn't contain the required info it will throw the exception. Also, if it passes the first check but the rate is < 0, it also throws an exception. So it doesn't actually look like you've done anything wrong.

    Looking further, I see it makes this call:

    $response = $this->request('https://api.fixer.io/latest?base='.$source.'&symbols='.$target);
    

    I translated this based on your code to:

    https://api.fixer.io/latest?base=USD&symbols=INR

    Which if you click that link, will give you the answer:

    0   "#################################################################################################################################"
    1   "#                                                                                                                               #"
    2   "# IMPORTANT - PLEASE UPDATE YOUR API ENDPOINT                                                                                   #"
    3   "#                                                                                                                               #"
    4   "# This API endpoint is deprecated and has now been shut down. To keep using the Fixer API, please update your integration       #"
    5   "# to use the new Fixer API endpoint, designed as a simple drop-in replacement.                                                  #"
    6   "# You will be required to create an account at https://fixer.io and obtain an API access key.                                   #"
    7   "#                                                                                                                               #"
    8   "# For more information on how to upgrade please visit our Github Tutorial at: https://github.com/fixerAPI/fixer#readme          #"
    9   "#                                                                                                                               #"
    a   "#################################################################################################################################"
    

    It looks like the library is out of date.

    Either use a different lib, or get it working again.

    To get around this, you can firstly create an account with the new api, sign up for github if you arent already, fork the repository, change this line https://github.com/viewflex/forex/blob/master/src/Forex.php#L53, and either send a pull request, or change the composer package name https://github.com/viewflex/forex/blob/master/composer.json#L2 and register your own new package with https://packagist.org/ (change all the file namespaces too if you do that.)

    If you sign up for the new API and send the new endpoint, I can send the guy a pull request with the fix.