Page 2 of 2

Re: How do we get Google's "Euro to Pound" conversion rate?

Posted: Mon Sep 19, 2016 9:27 am
by simonmlewis
Sure.... but I don't have the Euro value.......

Re: How do we get Google's "Euro to Pound" conversion rate?

Posted: Mon Sep 19, 2016 9:29 am
by Celauran
Erm, division? 1 EUR = 0.855 GBP, therefore 1 GBP = 1/0.855 EUR = 1.16959 EUR.

Re: How do we get Google's "Euro to Pound" conversion rate?

Posted: Mon Sep 19, 2016 9:45 am
by simonmlewis

Code: Select all

$xml = file_get_contents('http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml');
$sxe = new SimpleXMLElement($xml);

$sxe->registerXPathNamespace('r', 'http://www.ecb.int/vocabulary/2002-08-01/eurofxref');
$sxe->registerXPathNamespace('g', 'http://www.gesmes.org/xml/2002-08-01');
$cube = $sxe->xpath('r:Cube/r:Cube/r:Cube[@currency="GBP"]');
$eurocheck = $cube[0]['rate'];
echo "$row->price <b>$eurocheck</b>";

$europrice = $row->price / $euroeheck;
This is echoing 0. Well actually it keeps erroring saying it cannot divide by zero.
Yet if I echo the second line from botton, it displays the correct amounts.

It's just the dividing that wont work.

Re: How do we get Google's "Euro to Pound" conversion rate?

Posted: Mon Sep 19, 2016 9:47 am
by Celauran
Even with the $euroeheck typo corrected?

Re: How do we get Google's "Euro to Pound" conversion rate?

Posted: Mon Sep 19, 2016 9:50 am
by Celauran
Ah, I see what's happening. $eurocheck is an instance of SimpleXMLElement. You need to cast it to a string before using it.

Code: Select all

<?php

$xml = file_get_contents('http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml');
$sxe = new SimpleXMLElement($xml);

$sxe->registerXPathNamespace('r', 'http://www.ecb.int/vocabulary/2002-08-01/eurofxref');
$sxe->registerXPathNamespace('g', 'http://www.gesmes.org/xml/2002-08-01');
$cube = $sxe->xpath('r:Cube/r:Cube/r:Cube[@currency="GBP"]');
$eurocheck = (string) $cube[0]['rate'];

$europrice = 100 / $eurocheck;
echo $europrice;

Re: How do we get Google's "Euro to Pound" conversion rate?

Posted: Mon Sep 19, 2016 9:51 am
by simonmlewis
actually yes.

[text]14.99 0.85500
Warning: Division by zero in C:\xampp\phpMyAdmin\site-wide\includes\product.inc on line 559[/text]

Code: Select all

$xml = file_get_contents('http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml');
$sxe = new SimpleXMLElement($xml);

$sxe->registerXPathNamespace('r', 'http://www.ecb.int/vocabulary/2002-08-01/eurofxref');
$sxe->registerXPathNamespace('g', 'http://www.gesmes.org/xml/2002-08-01');
$cube = $sxe->xpath('r:Cube/r:Cube/r:Cube[@currency="GBP"]');
$eurocheck = $cube[0]['rate'];
echo "$row->price <b>$eurocheck</b>";

>> line 559 >> $europrice = $row->price / $eurocheck;

Re: How do we get Google's "Euro to Pound" conversion rate?

Posted: Mon Sep 19, 2016 10:52 am
by Celauran
I trust you saw the post above and this is sorted now?

Re: How do we get Google's "Euro to Pound" conversion rate?

Posted: Mon Sep 19, 2016 11:02 am
by simonmlewis
Sorry you've lost me now.
$europrice = $row->price / 0.85 (roughly).
So how do I reach that from the code you sent?

Re: How do we get Google's "Euro to Pound" conversion rate?

Posted: Mon Sep 19, 2016 11:07 am
by Celauran

Re: How do we get Google's "Euro to Pound" conversion rate?

Posted: Mon Sep 19, 2016 11:13 am
by simonmlewis
So Europrice becomes 116.95906432749.
I am missing something here. How do I go from 116.95, and take that and use it to convert £15 into Euros ?

Re: How do we get Google's "Euro to Pound" conversion rate?

Posted: Mon Sep 19, 2016 11:20 am
by Celauran
Instead of using 100, which I needed to use for the sake of example, you'd use your $row->price

Re: How do we get Google's "Euro to Pound" conversion rate?

Posted: Mon Sep 19, 2016 11:27 am
by simonmlewis
Oh I see.
So without duplicating the code and pulling it in twice, how would I use it for USD ?

Re: How do we get Google's "Euro to Pound" conversion rate?

Posted: Mon Sep 19, 2016 11:30 am
by Celauran
Might be worth setting up the XML parsing to be run on a schedule. Once a day you get the latest rates, parse them, and store in your DB. Then you can just reference the stored values to perform your calculations.

as for
So without duplicating the code
start using functions to keep your code DRY. Ultimately you'll want to wrap those into classes, but one step at a time.

Re: How do we get Google's "Euro to Pound" conversion rate?

Posted: Mon Sep 19, 2016 11:34 am
by simonmlewis
Yes I think a cron job would be better. That way if their page takes an age to load, it doesn't matter!

We already have a cron that runs regularly, so I could pop it into the same one.