Topic: No exchange rate working in FA now

Even the stable ECB exchange rate seems to fail in FA most of the time atleast here in India - not IP blocking but something else and from localhost on windows.

The ECB site page for the forex has a For developers link with php constructs used to acquire the exchange rate and all are in EURO as expected. The page has not changed but does not recognise the IP based url and redirects the http to https though.

Re: No exchange rate working in FA now

Should we use https://exchange-rates.org ?
Seems they have most currencies in the world and create their website for currency converting purpose.
They are also quite generous with having someone use their data by providing code to embed rates table.

Phuong

Re: No exchange rate working in FA now

Here is the code: https://drive.google.com/open?id=1yvXKr … ch0jnsdbvE
For the config file:

$xr_providers = array("EXCHANGE-RATES.ORG", "ECB", "YAHOO", "GOOGLE", "BLOOMBERG");

We should remove all current rate providers as well, they no longer allow to access their data without permission

Phuong

Re: No exchange rate working in FA now

I will have a look into this. Sounds great @notrinos. It looks that the new provider is sponsored by bank N26, Germany.

Joe

Re: No exchange rate working in FA now

This seems to be reliable and has been included in FA. Committed to stable repo. Thanks @notrinos.

new file: config-default.php can be downloaded here. (trailing tab in affected line to be fixed later)
new file: /gl/includes/db/gl_db_rates.inc can be downloaded here.

Joe

Re: No exchange rate working in FA now

ECB allows download of some rates at:
https://www.ecb.europa.eu/stats/eurofxref/eurofxref.zip

After extracting the CSV from the ZIP file, the following format of 32 exchange rates in Euros are there:

Date, USD, JPY, BGN, CZK, DKK, GBP, HUF, PLN, RON, SEK, CHF, ISK, NOK, HRK, RUB, TRY, AUD, BRL, CAD, CNY, HKD, IDR, ILS, INR, KRW, MXN, MYR, NZD, PHP, SGD, THB, ZAR, 
18 December 2018, 1.1377, 127.86, 1.9558, 25.753, 7.4675, 0.89715, 323.11, 4.2848, 4.6467, 10.2760, 1.1281, 139.00, 9.8835, 7.4048, 76.1678, 6.0782, 1.5810, 4.4366, 1.5249, 7.8455, 8.8976, 16472.76, 4.2812, 80.2530, 1284.35, 22.8293, 4.7579, 1.6556, 60.390, 1.5590, 37.277, 16.3106, 
Post's attachments

eurofxref.zip 410 b, 1 downloads since 2018-12-18 

You don't have the permssions to download the attachments of this post.

Re: No exchange rate working in FA now

https://fx-rate.net/USD/GBP/
Shows rate for 1000 units of source currency

Re: No exchange rate working in FA now

ECB exchange rate can be fixed by modifying from line 139 onwards in gl/includes/db/gl_db_rates.inc:

    if ($provider == 'ECB')
    {
        $filename = "/stats/eurofxref/eurofxref-daily.xml";
        $site = "www.ecb.europa.eu";
        $proto = 'https://';
        $site_ip="172.230.157.137";
        $contents=file_get_contents($proto.$site.$filename);
    }

Also remove the trailing tab character in the config.default.php file for the $xr_providers line.

Re: No exchange rate working in FA now

Exchange-rates.org is absolutely fine one.  It supports major 30 currencies in the world. But we have FA users from many small countries. We need to look up them as well.

So what I am suggesting is to adapt one more exchange rate provider. https://currencylayer.com or fixer.io to work on 168 currencies. They allow 1000 request per month for free of cost .  I hope that's more than enough for them. Because we keep existing exchange rates for next requests. So let's provide them one more as well. Also we can provide them functionality to go premium to get extra. But that's their choice. Hope Joe and janusz will think of it..

Subscription service based on FA
HRM CRM POS batch Themes

Re: No exchange rate working in FA now

https://free.currencyconverterapi.com/

They allow 100 Requests Per Hour for Free.

This is also a good option

config.php changes

$xr_providers = array("ECB", "YAHOO", "GOOGLE", "BLOOMBERG", "CCA");

gl_db_rates.inc changes as follows.

function get_extern_rate($curr_b, $provider = 'ECB', $date)
{
    global    $path_to_root;

    if ($date != Today())    // no historical rates available
        return 0;

    $contents = '';
    $proto = 'http://';
    $curr_a = get_company_pref('curr_default');
    if ($provider == 'ECB')
    {
        $filename = "/stats/eurofxref/eurofxref-daily.xml";
        $site = "www.ecb.europa.eu";
        $site_ip="172.230.157.137";
    }
    elseif ($provider == 'YAHOO')
    {
        $filename = "/d/quotes.csv?s={$curr_a}{$curr_b}=X&f=sl1d1t1ba&e=.csv"; // new URL's for YAHOO
        $site = "download.finance.yahoo.com";
        $site_ip="203.84.220.151";
    }
    elseif ($provider == 'GOOGLE')
    {
        $filename = "/bctzjpnsun/converter?a=1&from={$curr_a}&to={$curr_b}";
        $site = "finance.google.com";
    }
    elseif ($provider == 'BLOOMBERG')
    {
        $filename = "/quote/{$curr_b}{$curr_a}:CUR";
        $site = "www.bloomberg.com";
        $proto = 'https://';
        $contents=file_get_contents($proto.$site.$filename);
    }
    elseif ($provider == 'CCA') //free.currencyconverterapi.com
    {
        $filename = "/api/v5/convert?q={$curr_b}_{$curr_a}&compact=y";
        $site = "free.currencyconverterapi.com";
        $proto = 'https://';

    }
    if (empty($contents)) {
        if (function_exists('curl_init'))
        {    // first check with curl as we can set short timeout;
            $retry = 1;
             do {
                   $ch = curl_init();
                   curl_setopt ($ch, CURLOPT_URL, $proto.$site.$filename);
                   curl_setopt ($ch, CURLOPT_COOKIEJAR, VARLIB_PATH."/cookie.txt");
                   curl_setopt ($ch, CURLOPT_HEADER, 0);
                   curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
                   // prevent warning while safe_mode/open_basedir on (redirection doesn't occur at least on ECB page)
                   if (!ini_get('safe_mode') && !ini_get('open_basedir'))
                       curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
                   curl_setopt ($ch, CURLOPT_TIMEOUT, 3);
                   $contents = curl_exec ($ch);
                   curl_close($ch);
                // due to resolver bug in some curl versions (e.g. 7.15.5)
                // try again for constant IP.
                if (isset($site_ip))
                       $site=$site_ip;
               } while( ($contents == '') && $retry--);
        } else {
            $contents = url_get_contents($proto.$site.$filename);
        }
    }
    $val = '';
    if ($provider == 'ECB')
    {
        $contents = str_replace ("<Cube currency='USD'", " <Cube currency='EUR' rate='1'/> <Cube currency='USD'", $contents);
        $from_mask = "|<Cube\s*currency=\'" . $curr_a . "\'\s*rate=\'([\d.,]*)\'\s*/>|i";
        preg_match ( $from_mask, $contents, $out );
        $val_a = isset($out[1]) ? $out[1] : 0;
        $val_a = str_replace ( ',', '', $val_a );
        $to_mask = "|<Cube\s*currency=\'" . $curr_b . "\'\s*rate=\'([\d.,]*)\'\s*/>|i";
        preg_match ( $to_mask, $contents, $out );
        $val_b = isset($out[1]) ? $out[1] : 0;
        $val_b = str_replace ( ',', '', $val_b );
        if ($val_b)
        {
            $val = $val_a / $val_b;
        }
        else
        {
            $val = 0;
        }
    }
    elseif ($provider == 'YAHOO')
    {
        $array = explode(',',$contents); // New operations for YAHOO. Safer.
        $val = $array[1];
        if ($val != 0)
            $val = 1 / $val;
    }
    elseif ($provider == 'GOOGLE')
    {
        $val = getInnerStr($contents, '<span class=bld>', ' ');
        if (empty($val) || $val+0 <= 0.0001) {
            // reverse lookup on could not convert for too small values
            $filename = "/bctzjpnsun/converter?a=1&from={$curr_b}&to={$curr_a}";
            $contents = url_get_contents($proto.$site.$filename);
            $val = getInnerStr($contents, '<span class=bld>', ' ');
        } else {
            if ($val != 0)
                $val = 1 / $val;
        }
    }
    elseif ($provider == 'BLOOMBERG')
    {
        $val = getInnerStr($contents, ',"price":', ',"');
    }
    elseif ($provider == 'CCA')
    {
        $rates = json_decode($contents, true);
        $val = $rates[$curr_b.'_'.$curr_a]['val'];
      }
    return $val;
}  /* end function get_extern_rate */
www.boxygen.pk

Re: No exchange rate working in FA now

@joe: Please include the fixes to ECB in my previous post and @boxygen's currencyconverterapi.com (CCA) as exchange providers.

CCA's output of:

https://free.currencyconverterapi.com/api/v5/convert?q=USD_EUR&compact=y

is a json value formatted like:

{"USD_EUR":{"val":0.875505}}

The reverse currency rate check for large decimal differences like IDR => KWD is not accounted for in it as yet.

Re: No exchange rate working in FA now

Hello @apmuthu,

Yes I will include your fix and @boxygen's provider in FA later this evening.

Joe

Re: No exchange rate working in FA now

Hello @boxygen,

Your code for CCA doesn't work. It gives no rates. Will you give it a check, please? Thanks.

/Joe

Re: No exchange rate working in FA now

Fixed file gl_db_rates.inc for ECB and included CCA can be download here

for config file:

$xr_providers = array("ECB", "EXCHANGE-RATES.ORG", "CCA", "YAHOO", "GOOGLE", "BLOOMBERG");

A well-known financial institution should be default so I moved ECB to first position.

Phuong

Re: No exchange rate working in FA now

Sure @notrinos. Thanks for the investigation.

I will just test them all and commit them later.

/Joe

Re: No exchange rate working in FA now

Hello @notrinos

I got the following errors when using

Yahoo
Undefined offset: 1 in file: C:\wamp3\www\account24\gl\includes\db\gl_db_rates.inc at line 228
Google
Cannot retrieve exchange rate for currency GBP. Please adjust approximate rate if needed.
Bloomberg
Cannot retrieve exchange rate for currency GBP. Please adjust approximate rate if needed.

I think we could just commit it. The 3 first providers works OK. We can then investigate further on the others, right?

/Joe

Re: No exchange rate working in FA now

The 3 Providers, ECB, CCA and EXCHANGE-RATES.ORG is working now and the /gl/includes/db/gl_db_rates.inc is committed as well as config.default.php.
They can be downloaded here and here.

Please copy the $xr_providers array from config.default.php to your config.php.

/Joe

18 (edited by notrinos 03/21/2019 05:37:15 pm)

Re: No exchange rate working in FA now

Updated new gl_db_rates.inc

- Removed CCA, (currencyconverterapi.com) is no longer working
- Updated url from google api

Fixed file here

And new config.default.php content

$xr_providers = array("ECB", "EXCHANGE-RATES.ORG", "GOOGLE", "YAHOO", "BLOOMBERG");
Phuong

Re: No exchange rate working in FA now

This has been committed to stable repo. Thank you @notrinos for providing this.

Remember to change the settings in config.php also.

/Joe

20 (edited by boxygen 09/06/2021 04:55:44 pm)

Re: No exchange rate working in FA now

Hello, Recently due to some changes in output of EXCHANGE-RATES.ORG the current code is not retrieving Exchange Rate.

Following fix is needed.

/gl/includes/db/gl_db_rates.inc
Line # 240 Replace

$val = getInnerStr($contents, '<span id="ctl00_M_lblToAmount">', '<');
        $val = str_replace (',', '', $val);

with

        $val = getInnerStr($contents, "1 $curr_b = ",$curr_a);
www.boxygen.pk

Re: No exchange rate working in FA now

Thanks @boxygen.

Fixed and committed to stable repo.

A fixed file can be downloaded and replaced.

/Joe

Post's attachments

gl_db_rates.inc 8.5 kb, 4 downloads since 2021-09-07 

You don't have the permssions to download the attachments of this post.