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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

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

Post by simonmlewis »

Sure.... but I don't have the Euro value.......
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

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

Post by Celauran »

Erm, division? 1 EUR = 0.855 GBP, therefore 1 GBP = 1/0.855 EUR = 1.16959 EUR.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

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

Post 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.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

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

Post by Celauran »

Even with the $euroeheck typo corrected?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

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

Post 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;
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

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

Post 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;
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

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

Post by Celauran »

I trust you saw the post above and this is sorted now?
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

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

Post 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?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

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

Post 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 ?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

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

Post by Celauran »

Instead of using 100, which I needed to use for the sake of example, you'd use your $row->price
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

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

Post by simonmlewis »

Oh I see.
So without duplicating the code and pulling it in twice, how would I use it for USD ?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

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

Post 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.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

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

Post 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.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Post Reply