Forum Moderators: coopster

Message Too Old, No Replies

UK -> Euro Exchange rate feed

Need to automate the conversion on a site.

         

donovanh

12:42 pm on Jun 11, 2004 (gmt 0)

10+ Year Member



I'm building a site that has prices in UK£, and wish to display the prices in Euro automagically. I've been able to extract the conversion rate and store it in a text file, which the site files then refer to when calculating the Euro prices.

I'd like to be able to have this value in the text file updated via a php script which I could then set to run hourly. Does anyone know of a good source of this conversion rate? At the moment i have to manually update the text file on a daily basis, which is far from perfect ;)

Thanks,

Don

Sanenet

12:49 pm on Jun 11, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Unless you pay, I don't think this exists for free (I'd love to find a free source!). However, sites like xe.com offer free conversion tools.

You could always... ahem... nick it using a script?

jatar_k

4:39 pm on Jun 11, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



we use an automated cron that reads rates from, in our particular case, the bank of canada. They have a csv file available to read. We don't pay for this service.

You could check with some of the big banks and see if they have this available online.

dmorison

7:50 pm on Jun 11, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There's a free XML feed from the ECB:

[ecb.int...]

donovanh

9:00 pm on Jun 11, 2004 (gmt 0)

10+ Year Member



Nice one dmorison, thats perfect. (Gives me a good excuse to learn a little about processing xml with php too ;)

Many thanks!

Don

mat

9:03 am on Jun 12, 2004 (gmt 0)

10+ Year Member



Hey, a 'me too' post - thanks for that link dmorison, extremely useful. Made my day.

donovanh

8:21 pm on Jun 12, 2004 (gmt 0)

10+ Year Member



I've been looking for a way to get this XML data and use it, but what seemed like a very simple exercise at first, seems totally bewildering.

Is there an accepted way to grab an XML result, such as the above, and parse the results into an array? I'm trying to do so using NuSoap but it just isn't making any sense.

Any pointers much appreciated,

Don

dmorison

9:13 pm on Jun 12, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The easiest way to process XML data is using XPath statements. XPath allows you to reference the contents of an XML document almost as easily as you refer to directories and files on a disk.

Have a look at the PHPXPath project on Sourceforge:

[sourceforge.net...]

If you download phpxpath into a directory; you can get the EUR:GBP exchange rate using the following code:

<?php

require("XPath.class.php");

$xp = new XPath();

$xp->importFromFile("http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml");

$EUR_GBP_RATE = $xp->getAttributes("//Cube[@currency='GBP']","rate");

echo($EUR_GBP_RATE);

?>

donovanh

9:50 pm on Jun 12, 2004 (gmt 0)

10+ Year Member



Nice.

Thats exactly the kind of thing I was looking for. Thinking it would be much more complex, I just finished (5 mins ago) writing the following script to get the data via str_replace:


$euroinfo = 'http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml';

$fp = fopen($euroinfo, "r"); 

$data = ""; 

while(!feof($fp)) 

{ 

$data .= fgets($fp, 4096); 

if (preg_match ('/GBP/', $data)) {

$rate = $data;

}

$data = '';

} 

$rate = str_replace("<Cube currency='GBP' rate='", '', $rate);

$rate = str_replace("'/>", '', $rate);

$rate = str_replace("\t", '', $rate);

The XPath method is much tidier! :)

Don