Zen Cart Logo
Forums / Currencies & Sales Taxes, VAT, GST, etc. / Update currencies from timegenie.com's xml feed

Update currencies from timegenie.com's xml feed

Views: 6,113

Results 1 to 17 of 17
03 Feb 2012, 17:35
#1
simon1066 avatar

simon1066

Totally Zenned

Join Date:
Feb 2009
Location:
UK
Posts:
1,326
Plugin Contributions:
0

Update currencies from timegenie.com's xml feed

I wonder if someone can spare a few lines of code to alter admin/includes/functions/localization.php so that exchange rates are taken from timegenie's xml file rather than ecb's.

I've added this to init_general_funcs.php

define('CURRENCY_SERVER_PRIMARY', 'timegenie');

and have this at the top of localization.php

function quote_timegenie_currency($currencyCode = '', $base = DEFAULT_CURRENCY)
{
  $requested = $currencyCode;
  $url = 'http://rss.timegenie.com/forex.xml';
  $data = '';
  // check via file() ... may fail if php file Wrapper disabled.
  $XMLContent = @file($url);
  if (! is_object($XMLContent) && function_exists('curl_init')) {
    // check via CURL instead.
    $XMLContent = doCurlCurrencyRequest('POST', $url, $data);
    $XMLContent = explode("\n", $XMLContent);
  }
  $currencyArray = array();
  $rate = 1;
  $line = '';
//  $currencyCode = '';
  $currencyArray['EUR'] = 1;
  foreach ($XMLContent as $line) {
    if (preg_match("/currency='([[:alpha:]]+)'/", $line, $currencyCode)) {
      if (preg_match("/rate='([[:graph:]]+)'/", $line, $rate)) {
        $currencyArray[$currencyCode[1]] = (float)$rate[1];
      }
    }
  }
  if ($requested == $base) {
    $rate = 1;
  } else {
    $rate = (string)((float)$currencyArray[$requested] / $currencyArray[DEFAULT_CURRENCY]);
  }
  return $rate;
}

timegenie's xml format is

<credit>
<!--
In order to use this data feed from timegenie.com, you have to display the following text, or something similar, clearly visible on your web page. The text should be a link to www.timegenie.com
-->
<!--
Please read more about our conditions and guidelines at http://rss.timegenie.com/free_xml_csv_feeds
-->
<text>Courtesy of Time Genie</text>
<url>http://www.timegenie.com/</url>
<title>foreign exchange rates courtesy of Time Genie</title>
</credit>
<data>
<code>BSD</code>
<description>Bahamas Dollar</description>
<rate>1.3176</rate>
</data>

whereas the existing ecb's format was

<Cube currency="USD" rate="1.3160"/>

I'm unsure as to how to make the necessary edits, although this has prompted me to start learning more about php.

04 Feb 2012, 15:19
#2
simon1066 avatar

simon1066

Totally Zenned

Join Date:
Feb 2009
Location:
UK
Posts:
1,326
Plugin Contributions:
0

Re: Update currencies from timegenie.com's xml feed

From what I've gathered so far, it appears that I should probably not try parsing timegenie's feed using regular expression, as is the existing method for the ecb feed, due to its nested tags. Instead use an xml parser - need to look into this.

05 Feb 2012, 00:51
#3
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,513
Plugin Contributions:
177

Re: Update currencies from timegenie.com's xml feed

Why is it you want to use timegenie specifically?
What about their terms and conditions?

07 Feb 2012, 09:57
#4
simon1066 avatar

simon1066

Totally Zenned

Join Date:
Feb 2009
Location:
UK
Posts:
1,326
Plugin Contributions:
0

Re: Update currencies from timegenie.com's xml feed

Timegenie provides a more comprehensive list of currencies than those on currently updated by zencart.

As far as I can see, their terms are simply to provide attribution, which I don't see as a problem.

07 Feb 2012, 20:07
#5
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,513
Plugin Contributions:
177

Re: Update currencies from timegenie.com's xml feed

I'll be happy to take a look, but it might be several days til I get to it.

08 Feb 2012, 16:57
#6
simon1066 avatar

simon1066

Totally Zenned

Join Date:
Feb 2009
Location:
UK
Posts:
1,326
Plugin Contributions:
0

Re: Update currencies from timegenie.com's xml feed

Thank you. Absolutely no rush - I'll obviously be updating manually in the meantime.

09 Feb 2012, 10:57
#7
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,513
Plugin Contributions:
177

Re: Update currencies from timegenie.com's xml feed

/admin/includes/functions/extra_functions/currency_timegenie.php```

<?php /** * @package admin * @copyright Copyright 2003-2012 Zen Cart Development Team * @copyright Portions Copyright 2003 osCommerce * @license http://www.zen-cart.com/license/2_0.txt GNU Public License V2.0 * @version $Id: currencyupdate_timegenie.php 2012-02-09 drbyte $ */ /** * This allows you to extract currency exchange rates from TimeGenie * YOU MUST GIVE CREDIT TO THEM VIA A LINK ON YOUR SITE. THIS IS REQUIRED ACCORDING TO THEIR TERMS AND CONDITIONS: * Please read more about TimeGenie conditions and guidelines at http://rss.timegenie.com/foreign_exchange_rates_forex * * In order to use this data feed from timegenie.com, you have to display the following text, or something similar, clearly visible on your web page. The text should be a link to www.timegenie.com * Foreign exchange rates courtesy of Time Genie http://www.timegenie.com/ */ /** * EUR|Euro|1 */ function quote_timegenie_currency($code, $base = DEFAULT_CURRENCY) { if ($code == $base) return 1; $url = 'http://rss.timegenie.com/forex.txt'; // check via file() ... may fail if php file Wrapper disabled. $page = @file($url); if ((!isset($page) || $page == '') && function_exists('curl_init')) { // check via cURL instead. $page = doCurlCurrencyRequest('GET', $url); $page = explode("\n", $page); } if ($page !='') { $currencyArray = array(); foreach($page as $key) { $val = explode('|', $key); $currencyArray[$val[0]] = (float)$val[2]; } return (string)((float)$currencyArray[$code] / $currencyArray[DEFAULT_CURRENCY]); } } ``` /admin/includes/extra_configures/currency_timegenie.php``` <?php /** * @package admin * @copyright Copyright 2003-2012 Zen Cart Development Team * @copyright Portions Copyright 2003 osCommerce * @license http://www.zen-cart.com/license/2_0.txt GNU Public License V2.0 * @version $Id: currencyupdate_timegenie.php 2012-02-09 drbyte $ */ if (!defined('IS_ADMIN_FLAG')) { die('Illegal Access'); } define('CURRENCY_SERVER_PRIMARY', 'TimeGenie'); ``` YOU MUST GIVE CREDIT TO THEM VIA A LINK ON YOUR SITE. THIS IS REQUIRED ACCORDING TO THEIR TERMS AND CONDITIONS: See and follow their conditions and guidelines at <http://rss.timegenie.com/foreign_exchange_rates_forex>
10 Feb 2012, 06:58
#8
brevardcountypcr avatar

brevardcountypcr

New Zenner

Join Date:
Nov 2011
Posts:
67
Plugin Contributions:
0

Re: Update currencies from timegenie.com's xml feed

nice work :cool:

10 Feb 2012, 14:39
#9
simon1066 avatar

simon1066

Totally Zenned

Join Date:
Feb 2009
Location:
UK
Posts:
1,326
Plugin Contributions:
0

Re: Update currencies from timegenie.com's xml feed

That's great, thanks for doing this.

10 Feb 2012, 15:18
#10
simon1066 avatar

simon1066

Totally Zenned

Join Date:
Feb 2009
Location:
UK
Posts:
1,326
Plugin Contributions:
0

Re: Update currencies from timegenie.com's xml feed

The reason for my original question is that I'm carrying out an exercise to include all of the world's currencies in a zen cart site.

In order to maximise the automatic update of exchange rates and avoiding paid subscriptions I believe the best way is to gather rates from Timegenie, Ecb & Boc. I've successfully added a 'tertiary' server reference but am having trouble with Boc's feed.

On a 'pre-timegenie' copy of the site and setting Boc as the primary server the automatic update only gets the rate for USD and errors out on the others - picking them up from the secondary server.

Could it be that the code for retrieving the Boc feed needs revising?

Has anyone else been able to retrive rates from this source?

11 Feb 2012, 07:19
#11
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,513
Plugin Contributions:
177

Re: Update currencies from timegenie.com's xml feed

It appears that BOC has changed their data feeds.

function quote_boc_currency($currencyCode = '', $base = DEFAULT_CURRENCY)
{
  if ($currencyCode == $base) return 1;
  static $CSVContent;
  $requested = $currencyCode;
  $url = 'http://www.bankofcanada.ca/stats/assets/csv/fx-seven-day.csv';
  $currencyArray = array();
  $currencyArray['CAD'] = 1;
  if (!isset($CSVContent) || $CSVContent == '') {
    $CSVContent = file($url);
    if (! is_object($CSVContent) && function_exists('curl_init')) {
      $CSVContent = doCurlCurrencyRequest('GET', $url);
      $CSVContent = explode("\n", $CSVContent);
    }
  }
  foreach ($CSVContent as $line) {
    if (substr($line, 0, 1) == '#' || substr($line, 0, 4) == 'Date' || trim($line) == '') continue;
    $data = explode(',', $line);
    $curName = $data[1];
    $curRate = $data[sizeof($data)-1];
    $currencyArray[trim($curName)] = (float)$curRate;
  }
  $rate = (string)($currencyArray[DEFAULT_CURRENCY]/(float)$currencyArray[$requested]);
  return $rate;
}

These updates will be in v1.5.1*

11 Feb 2012, 10:50
#12
simon1066 avatar

simon1066

Totally Zenned

Join Date:
Feb 2009
Location:
UK
Posts:
1,326
Plugin Contributions:
0

Re: Update currencies from timegenie.com's xml feed

Thank you, that works well.

Having carried out some checks on the updated exchange rates I see that the Boc values in 'fx-seven-day.csv' are not referenced to one currency such as CAD but give the value in CAD for 1 unit of each countries' currency. i.e. from the .csv, 1 Croatian Kuna = 0.1741 CAD, what zen cart needs is 1 CAD = 5.7408 Croatian Kuna. Therefore the reciprocal values of the Boc supplied exchange rates are required.

Perhaps this calculation can be incorporated into the code somewhere. If not I may have to do without that source.

12 Feb 2012, 19:21
#13
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,513
Plugin Contributions:
177

Re: Update currencies from timegenie.com's xml feed

grr - I'd posted the wrong update. Corrected above.

13 Feb 2012, 09:22
#14
simon1066 avatar

simon1066

Totally Zenned

Join Date:
Feb 2009
Location:
UK
Posts:
1,326
Plugin Contributions:
0

Re: Update currencies from timegenie.com's xml feed

Thank you for sorting all this out.

I can report that the Timegenie, Boc & Ecb feeds are all working and that Zen Cart is able to populate, from free sources, the exchange rates for 61% of the world's currencies.

07 Jan 2013, 10:26
#16
chris3j13 avatar

chris3j13

New Zenner

Join Date:
Jan 2013
Posts:
1
Plugin Contributions:
0

Re: Update currencies from timegenie.com's xml feed

Hi Dr Byte
I am new here. I try to put more than one currencies in my website to show the price in different currency. I notice it does not update the exchange rate automatically, then I download your currency timegenie 150 and plugin to my admin folder. also I put a tat in my side "source timegenie.com", I still can not make it work . I have try to make the default currency either USA or AUD. Can you help ?

06 Jun 2013, 18:28
#17
joyjoy avatar

joyjoy

Totally Zenned

Join Date:
Sep 2010
Posts:
611
Plugin Contributions:
0

Re: Update currencies from timegenie.com's xml feed

DrByte:

New plugins uploaded:
TimeGenie - http://www.zen-cart.com/downloads.php?do=file&id=1505
GoogleFinance - http://www.zen-cart.com/downloads.php?do=file&id=1506

Hello! Thank you for providing this mod! I just have a question. Does it update the rates automatically within the admin section or do I need to do anything else?