[SOLVED] Currency converter not working

Discussion in 'General Support' started by pani100, Nov 25, 2013.

  1. pani100

    pani100 Well-Known Member

    Joined:
    May 9, 2011
    Messages:
    2,327
    Likes Received:
    449
    Just a little note on the currency converter.
    Unfortunately the API stopped working on Nov. 1st 2013.
    By the looks of things google has stopped this service, so it will need to be replaced with something else.
    Until then currency converter will not work.
     
  2. jjjason84

    jjjason84 Donor Donor

    Joined:
    Jun 26, 2012
    Messages:
    702
    Likes Received:
    62
    leave it up to google to mess things up. :mad:
    I really did not want to re-script a new currency converter to my webid. :mad:
    Well i guess i got to add that to my list of things to do after i finish the digital item auction mod on my webid.

    I did find out that google still has a currency converter running at
    https://www.google.com/finance/converter?

    Here some info i found that may help re-scripting the currency converter
    http://www.chazzuka.com/php-currency-converter-via-google-finance-104/
    http://stackoverflow.com/questions/...-converter-api-will-it-shut-down-with-igoogle

    You can maybe change the link on the webid converter.inc.php page and do a little re-scripting to get the currency converter working again.

    Thank pani for the info
     
    Last edited: Nov 26, 2013
  3. jjjason84

    jjjason84 Donor Donor

    Joined:
    Jun 26, 2012
    Messages:
    702
    Likes Received:
    62
    Hello pani

    I found a fix for this problem and is very easy to fix and still uses google but not igoogle api.

    This should work in v1.1.0 webid

    Open includes/converter.inc.php on line 111 find this code
    PHP:
    //Call Google API
        
    $google_url "http://www.google.com/ig/calculator?hl=en&q=1" $fromCurrency "=?" $toCurrency;
        
    //Get and Store API results into a variable
        
    $result file_get_contents($google_url);
        
    //Explode result to convert into an array
        
    $result explode('"'$result);
        
    // get value
        
    $converted_amount explode(' '$result[3]);
        return 
    $converted_amount[0];
    Replace it with this one
    PHP:
    //Call Google API
        
    $fromCurrency urlencode($fromCurrency);
        
    $toCurrency urlencode($toCurrency);
        
    $get file_get_contents("https://www.google.com/finance/converter?a=1&from=$fromCurrency&to=$toCurrency");
        
    $get explode("<span class=bld>",$get);
        
    $get explode("</span>",$get[1]);  
        
    $converted_amount preg_replace("/[^0-9\.]/"null$get[0]);    
        return 
    $converted_amount;
    save and close converter.inc.php
    upload the converter.inc.php
    All Done go and test it out it should be working and with the correct currency.
     
  4. pani100

    pani100 Well-Known Member

    Joined:
    May 9, 2011
    Messages:
    2,327
    Likes Received:
    449
    Nice one jjjason84,
    I was in the middle of re-doing the converter a different way, actually 2 other different ways with one being a database insert of the rates once a day.
     
  5. jackworm

    jackworm New Member

    Joined:
    Dec 9, 2013
    Messages:
    6
    Likes Received:
    2
    I couldn't get this to work. From what I have read, google took down the converter. It is easy to swap over to the yahoo currency converter though if anyone else has the same problem, which is what I have done. Just google simple currency converter yahoo to get the script. Basically call the site, open the file that is returned and read the contents
     
  6. Grinfeld

    Grinfeld Member

    Joined:
    Apr 8, 2012
    Messages:
    64
    Likes Received:
    4
    Excellent jjjason84!! It works great in Webid 1.3.0.
    Thanks a lot,
    Mario Grinfeld
     
    Trip Kan likes this.
  7. david62311

    david62311 Well-Known Member

    Joined:
    Aug 29, 2013
    Messages:
    2,149
    Likes Received:
    247
    Great job jjjason84!!! I got mine to work now too. I had to replace my includes/converter.inc.php file with the original webid 1.1.1 version because whatever I had in there gave me errors. Once I added your code to the original code then it worked good.

    Is there filtering in the converter form to prevent mischief?
     
    Last edited: Nov 26, 2014
  8. GayzeN

    GayzeN New Member

    Joined:
    Feb 21, 2012
    Messages:
    20
    Likes Received:
    0
    Hi, I'm using WeBid 1.1.1, with an unmodified converter.inc.php file and found the same problem as listed above. I tried the fix listed by jjjason84, uploaded the edited file, cleared my cache ... and the converter results are still returning "0.00" regardless of the amounts and currencies I've entered.

    I have copied/pasted the above fix directly from this page. I don't think I'm doing anything wrong, but I'm no programmer, so "you never know".

    Can anyone offer any further suggestions to get the fix to work? (I've tried clearing browser cache, and accessing the site in a different browser, as well, with the same results ... or lack thereof.)

    Thanks in advance for any help you might offer!
     
  9. super_st

    super_st Well-Known Member

    Joined:
    Dec 2, 2012
    Messages:
    634
    Likes Received:
    141
    I had this issue in 1.1.1 but added the fix from post #3 and now works great.

    GayzeN, did you clear cache in admin panel?
     
    Last edited: Feb 9, 2015
  10. GayzeN

    GayzeN New Member

    Joined:
    Feb 21, 2012
    Messages:
    20
    Likes Received:
    0
    Thank you, super_st, for your reply.

    Yes, I did clear the cache (several times ... I tried re-uploading afterward just to be sure the file transferred cleanly and cleared it again afterward). I'm still getting 0.00 as the result when I try to use the converter.

    I'm baffled.
     
  11. GayzeN

    GayzeN New Member

    Joined:
    Feb 21, 2012
    Messages:
    20
    Likes Received:
    0
    Sorry to post again. I was hoping that rebooting the computer/overnight rest would have made a difference (though I realize that doesn't make a lot of sense), but alas....

    Every other fix I've applied, after searching this forum, has worked fine on my installation. You folks are an outstanding source of help, and that is why I returned to WeBid when another need for an auction arose. We used it several years ago to help a friend out of a difficult situation, and it all went very well.

    I'm stumped as to why this fix won't work. I'm still unable to get the converter to return anything but 0.00.

    This will sound silly, but those above who applied the fix successfully -- is it still working as of today? Could Google have pulled a bait and switch in the meantime? :)

    Is there, by any chance, a setting in the admin panel that needs to be set? I have my main location set to the USA, but the currency for the auction that is coming up defaults to Canada -- could that cause a problem?

    Our auction starts on March 8th so I do have time to play with this and try to get it working (it's a benefit auction being run by friends to help a friend who has been battling cancer for the last three years -- I am in the US and she is in Canada, thus the default currency/physical location of the site mismatch; friends from around the world want to participate, so it would be so helpful to have the converter working).

    Thanks again for any help you can offer.
     
    Last edited: Feb 10, 2015
  12. super_st

    super_st Well-Known Member

    Joined:
    Dec 2, 2012
    Messages:
    634
    Likes Received:
    141
    I only noticed it didnt work after i saw your forum post, i added code above and all is fine, see if you get same results, heres a link to mine http://drivenet-media.co.uk/auction/converter.php?AMOUNT=59.99

    not sure about the location to currency point as mine relates to one another, uk-gbp, but dont see why that would make a difference as you can choose in the converter anything you want
     
  13. GayzeN

    GayzeN New Member

    Joined:
    Feb 21, 2012
    Messages:
    20
    Likes Received:
    0
    Thank you again for taking the time to reply!

    I've tried using the code in the above fix three times now without success. I see it works perfectly fine on your site.

    I sure wish I could figure out what I'm doing wrong.

    My converter page is here: http://www.gazehound.com/auction/converter.php?AMOUNT=3.00

    Is it okay to post/quote the contents of my converter.inc.php file here?
     
    Last edited: Feb 10, 2015
  14. pani100

    pani100 Well-Known Member

    Joined:
    May 9, 2011
    Messages:
    2,327
    Likes Received:
    449
    Yes of course, this whole web site is about the code...
    This is what you should have in converter.inc.php

    PHP:
    <?php
    /***************************************************************************
     *   copyright                : (C) 2008 - 2013 WeBid
     *   site                    : http://www.webidsupport.com/
     ***************************************************************************/

    /***************************************************************************
     *   This program is free software; you can redistribute it and/or modify
     *   it under the terms of the GNU General Public License as published by
     *   the Free Software Foundation; either version 2 of the License, or
     *   (at your option) any later version. Although none of the code may be
     *   sold. If you have been sold this script, get a refund.
     ***************************************************************************/

    if (!defined('InWeBid')) exit();

    include 
    $include_path 'currencies.php';

    function 
    CurrenciesList()
    {
        global 
    $system$DBPrefix;

        
    $query "SELECT * FROM " $DBPrefix "rates";
        
    $res mysql_query($query);
        
    $system->check_mysql($res$query__LINE____FILE__);
        
    $CURRENCIES = array();
        while (
    $row mysql_fetch_assoc($res))
        {
            
    $CURRENCIES[$row['symbol']] = $row['valuta'];
        }
        return 
    $CURRENCIES;
    }

    function 
    ConvertCurrency($FROM$INTO$AMOUNT)
    {
        global 
    $conversionarray;

        
    $data = array(
            
    'amount'    => $AMOUNT,
            
    'from'         => $FROM,
            
    'to'         => $INTO
            
    );
        if (
    $FROM == $INTO) return $AMOUNT;

        
    $CURRENCIES CurrenciesList();

        
    $rate findconversionrate($FROM$INTO);
        if (
    $rate == 0)
        {
            
    $conversion googleconvert($AMOUNT$FROM$INTO);
            
    $conversionarray[1][] = array('from' => $FROM'to' => $INTO'rate' => $conversion);
            
    buildcache($conversionarray[1]);
            return 
    $AMOUNT $conversion;
        }
        else
        {
            return 
    $AMOUNT $rate;
        }
    }

    function 
    buildcache($newaarray)
    {
        global 
    $include_path;

        
    $output_filename $include_path 'currencies.php';
        
    $output "<?php\n";
        
    $output.= "\$conversionarray[] = '" time() . "';\n";
        
    $output.= "\$conversionarray[] = array(\n";

        for (
    $i 0$i count($newaarray); $i++)
        {
            
    $output .= "\t" "array('from' => '" $newaarray[$i]['from'] . "', 'to' => '" $newaarray[$i]['to'] . "', 'rate' => '" floatval($newaarray[$i]['rate']) . "')";
            if (
    $i < (count($newaarray) - 1))
            {
                
    $output .= ",\n";
            }
            else
            {
                
    $output .= "\n";
            }
        }

        
    $output .= ");\n?>\n";

        
    $handle fopen($output_filename'w');
        
    fputs($handle$output);
        
    fclose($handle);
    }

    function 
    findconversionrate($FROM$INTO)
    {
        global 
    $conversionarray;

        if (
    time() - (3600 24) < $conversionarray[0])
        {
            for (
    $i 0$i count($conversionarray[1]); $i++)
            {
                if (
    $conversionarray[1][$i]['from'] == $FROM && $conversionarray[1][$i]['to'] == $INTO)
                    return 
    $conversionarray[1][$i]['rate'];
            }
        }
        else
        {
            
    $conversionarray = array(0, array());
        }
        return 
    0;
    }

    function 
    googleconvert($amount$fromCurrency $toCurrency)
    {
        
    //Call Google API 
        
    $fromCurrency urlencode($fromCurrency); 
        
    $toCurrency urlencode($toCurrency); 
        
    $get file_get_contents("https://www.google.com/finance/converter?a=1&from=$fromCurrency&to=$toCurrency"); 
        
    $get explode("<span class=bld>",$get); 
        
    $get explode("</span>",$get[1]);   
        
    $converted_amount preg_replace("/[^0-9\.]/"null$get[0]);     
        return 
    $converted_amount
    }
    ?>
     
    Last edited: Feb 10, 2015
  15. GayzeN

    GayzeN New Member

    Joined:
    Feb 21, 2012
    Messages:
    20
    Likes Received:
    0
    I've opened your code above and my converter.inc.php file side by side in Dreamweaver and gone line by line over both. I can find no differences. :-( Am I missing something? This is my file, as active on my server. It was just the default file as unpacked from the v1.1.1 zip archive, with the "fix" code replacing the old Google Converter code, as in message #3 -- the same fix that seems to have worked for everyone else. I'm starting to feel unloved. LOL!


    PHP:
    <?php
    /***************************************************************************
     *   copyright                : (C) 2008 - 2013 WeBid
     *   site                    : http://www.webidsupport.com/
     ***************************************************************************/


    /***************************************************************************
     *   This program is free software; you can redistribute it and/or modify
     *   it under the terms of the GNU General Public License as published by
     *   the Free Software Foundation; either version 2 of the License, or
     *   (at your option) any later version. Although none of the code may be
     *   sold. If you have been sold this script, get a refund.
     ***************************************************************************/


    if (!defined('InWeBid')) exit();


    include 
    $include_path 'currencies.php';


    function 
    CurrenciesList()
    {
        global 
    $system$DBPrefix;


        
    $query "SELECT * FROM " $DBPrefix "rates";
        
    $res mysql_query($query);
        
    $system->check_mysql($res$query__LINE____FILE__);
        
    $CURRENCIES = array();
        while (
    $row mysql_fetch_assoc($res))
        {
            
    $CURRENCIES[$row['symbol']] = $row['valuta'];
        }
        return 
    $CURRENCIES;
    }


    function 
    ConvertCurrency($FROM$INTO$AMOUNT)
    {
        global 
    $conversionarray;


        
    $data = array(
            
    'amount'    => $AMOUNT,
            
    'from'         => $FROM,
            
    'to'         => $INTO
            
    );
        if (
    $FROM == $INTO) return $AMOUNT;


        
    $CURRENCIES CurrenciesList();


        
    $rate findconversionrate($FROM$INTO);
        if (
    $rate == 0)
        {
            
    $conversion googleconvert($AMOUNT$FROM$INTO);
            
    $conversionarray[1][] = array('from' => $FROM'to' => $INTO'rate' => $conversion);
            
    buildcache($conversionarray[1]);
            return 
    $AMOUNT $conversion;
        }
        else
        {
            return 
    $AMOUNT $rate;
        }
    }


    function 
    buildcache($newaarray)
    {
        global 
    $include_path;


        
    $output_filename $include_path 'currencies.php';
        
    $output "<?php\n";
        
    $output.= "\$conversionarray[] = '" time() . "';\n";
        
    $output.= "\$conversionarray[] = array(\n";


        for (
    $i 0$i count($newaarray); $i++)
        {
            
    $output .= "\t" "array('from' => '" $newaarray[$i]['from'] . "', 'to' => '" $newaarray[$i]['to'] . "', 'rate' => '" floatval($newaarray[$i]['rate']) . "')";
            if (
    $i < (count($newaarray) - 1))
            {
                
    $output .= ",\n";
            }
            else
            {
                
    $output .= "\n";
            }
        }


        
    $output .= ");\n?>\n";


        
    $handle fopen($output_filename'w');
        
    fputs($handle$output);
        
    fclose($handle);
    }


    function 
    findconversionrate($FROM$INTO)
    {
        global 
    $conversionarray;


        if (
    time() - (3600 24) < $conversionarray[0])
        {
            for (
    $i 0$i count($conversionarray[1]); $i++)
            {
                if (
    $conversionarray[1][$i]['from'] == $FROM && $conversionarray[1][$i]['to'] == $INTO)
                    return 
    $conversionarray[1][$i]['rate'];
            }
        }
        else
        {
            
    $conversionarray = array(0, array());
        }
        return 
    0;
    }


    function 
    googleconvert($amount$fromCurrency $toCurrency)
    {
    //Call Google API 
        
    $fromCurrency urlencode($fromCurrency); 
        
    $toCurrency urlencode($toCurrency); 
        
    $get file_get_contents("https://www.google.com/finance/converter?a=1&from=$fromCurrency&to=$toCurrency"); 
        
    $get explode("<span class=bld>",$get); 
        
    $get explode("</span>",$get[1]);   
        
    $converted_amount preg_replace("/[^0-9\.]/"null$get[0]);     
        return 
    $converted_amount;  
    }
    ?>


     
    Last edited by a moderator: Feb 11, 2015
  16. GayzeN

    GayzeN New Member

    Joined:
    Feb 21, 2012
    Messages:
    20
    Likes Received:
    0
    pani100, thank you so much. I've replied with the contents of my file, but the server responded that my reply was awaiting moderation (I would imagine that is because code was included in the post). As far as I could see, though, the file I have on my server and the code you put in your post above are identical. I included it in that response (which I'm sure will show up soon) just to see if I missed something when I manually compared.

    I really appreciate everyone's efforts to help.
     
  17. nay27uk

    nay27uk Super Moderator Staff Member

    Joined:
    Nov 24, 2009
    Messages:
    5,572
    Likes Received:
    547
    It was placed in a MODERATION QUEUE because you chose the CODE tags instead of the PHP tags when posting your post.

    I have edited the post for you to put the PHP tags in, instead of CODE tags and accepted the post.

    A tip for future reference is to hover over each icon and a tooltip will appear saying what it is for.

    CODE is for HTML code.

    PHP is for PHP etc.

    If you're posting PHP then please select the icon that says PHP and not CODE.

    If the PHP tag does not look right when posted (we are having problems with it at the moment) then edit your post within 1 hour and choose the quote icon instead.

    The CODE icon is to post HTML ONLY and anything posted using the CODE icon is put in to instant MODERATION until the code that has been posted is checked for VULNERABILITIES this is because allowing HTML to be posted anywhere on the internet is a severe security risk.
     
    Last edited: Feb 11, 2015
  18. GayzeN

    GayzeN New Member

    Joined:
    Feb 21, 2012
    Messages:
    20
    Likes Received:
    0
    Thank you so very much for helping with this. I had actually attempted to use the "PHP" wrapper several times, but each time I did, in the preview it broke the line-breaks and all of the code was run together in a big chunk. It was only when I substituted general "code" rather than "PHP" that I was able to get the post to keep the line breaks and post in a readable format.

    I really do appreciate you taking the time to fix it for me!
     
  19. pani100

    pani100 Well-Known Member

    Joined:
    May 9, 2011
    Messages:
    2,327
    Likes Received:
    449
    Right there is something not right then.
    Lets trace it back and see if we can find where it fails.
    You can start from stage 2 as I suspect the problem might be there (90% possibility).
    If you get no conversion rate from stage 2 continue to stage 1 and report back.


    STAGE 1.
    The converter uses a few additional scripts and can get confusing
    It seems like the call is being passed ok, the 'from' and 'to' are accepted but the $amount is where it fails.
    The converter uses ajax to return a response.
    It loads google_converter.js
    This initiates an ajax call when we click convert to ajax.php which in turn calls the includes/functions_ajax.php
    I am pretty sure there is no problem with the data sent as the amount does get passed through into the ajax call and it is returned (the amount we want to convert gets added into the ajax response on the top line).
    I would say one of the functions used to convert the amount is failing.
    But which one?

    The result we get returned comes from this line in includes/functions_ajax.php around line 43

    PHP:
    // construct string
        
    echo $amount ' ' $CURRENCIES[$from] . ' = ' $system->print_money_nosymbol($conversiontrue) . ' ' $CURRENCIES[$to];
    Change it to something like

    PHP:
    // construct string
        //echo $amount . ' ' . $CURRENCIES[$from] . ' = ' . $system->print_money_nosymbol($conversion, true) . ' ' . $CURRENCIES[$to];
        
    echo 'I am trying to convert ' $amount ' ' $from ' to ' $to' but receiving ' $conversion;
    You should get something like
    I am trying to convert 0.99 GBP to ARS but receiving 13.077999 (this will be the conversion rate)
    but most likely you will get
    I am trying to convert 0.99 GBP to ARS but receiving 0

    If the amount receiving is not working it is failing in the function : ConvertCurrency($from, $to, $amount);
    Check the $from and $to values from the above response. They should be the currency code (3 letter code) for each currency.


    STAGE 2
    I suspect in includes/converter.inc.php the function ConvertCurrency is failing.

    If you try to convert to the same currency, the amount works and gets passed
    from
    PHP:
    if ($FROM == $INTO) return $AMOUNT;
    Now to continue

    in the same function (includes/converter.inc.php -->around line 47 ) find

    PHP:
    $rate findconversionrate($FROM$INTO);
    and replace with

    PHP:
    $rate 0;
    This will force the googleconvert function

    Check to see if you get a rate now.
    If you do get a rate then there is a problem with the includes/currencies.php.
    This file is changed on each conversion. It saves the rate so it does not need to use the googleconvert if the same call has been made in the last day.
    Post your includes/currencies.php (if it exists).

    If you do not get a rate then there is a problem with the googleconvert function.

    Check and report back
     
    Last edited: Feb 11, 2015
  20. GayzeN

    GayzeN New Member

    Joined:
    Feb 21, 2012
    Messages:
    20
    Likes Received:
    0
    I performed Stage 2, and got no conversion rate. The same lack of result as the original problem; the answer returns "0".

    I then progressed back to Stage 1 and received the following:

    "I am trying to convert 3 CAD to GBP but receiving 0"

    If I convert Canadian dollars to Canadian dollars, I do receive the original amount instead of the 0.

    I'm not entirely sure what I'm looking for when you say:
    Do you mean just the currency codes in the output? If so, I've pasted that above, and they are outputting correctly.

    Thanks again, so much, for your time!

    PS, I did clear the cache in the admin interface after each file change.
     

Share This Page