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:

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

Post by simonmlewis »

We want to be able to update our website dynamically with the Euro price as it seems to change often.
We show the price, and say "in euros it is approx... X".
We want this euro price to be based on the conversion rate on Google, wthout having to keep an eye on the rate outselves.

How is it done?
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 »

Not Google, but the European Central Bank has a free feed that gets updated daily.
http://www.ecb.europa.eu/stats/eurofxre ... -daily.xml
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 »

I've no idea how to get into XML and export to a PHP variable. Can you please advise?
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 »

Use a remote call to get the XML -- Guzzle, cURL, file_get_contents, doesn't really matter -- and then parse it with SimpleXMLElement.
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 »

Ok. It's all a little beyond me.
I use file_get_contents for another purpose, but it just finds a particular numeric value and passes that to a $matches[0] variable.
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 »

I'm trying.... but am def going wrong

Code: Select all

$xmleuro = "http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml";
$contentseuro = file_get_contents($xmleuro);
$eurofind = new SimpleXMLElement($xmleuro);
echo $Cube[5]->currency;
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 »

You've got nested Cube elements which you need to traverse. Building off your example,

Code: Select all

$xmleuro = "http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml";
$contentseuro = file_get_contents($xmleuro);
$eurofind = new SimpleXMLElement($xmleuro);
echo $eurofind->Cube[0]->Cube[0]->Cube[5]->currency;
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 »

That's really quick and dirty, though, and quite brittle. Look into XPath and namespaces. I need to run off to the office now but will check back later.
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 »

My local version doesn't like:

Code: Select all

$eurofind = new SimpleXMLElement($xmleuro);
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 »

simonmlewis wrote:My local version doesn't like:

Code: Select all

$eurofind = new SimpleXMLElement($xmleuro);
What does that even mean?
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 »

Using the XPath method I mentioned earlier, you'd do something like this:

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"]');
echo $cube[0]['rate'];
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 »

Ok. I think I may have done something the wrong way around.
I need to get the value to convert GBP to EURO?
And to DOLLAR.
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 »

You've got EURO to GBP, which means you also have GBP to EURO. You've also got EURO to USD. You can math it out easily enough.
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 »

To get a euro from gbp you do Pound Euro. So how do I say "£55.00 i approx x Euro"? Without knowing it's around 1.17 euro to the pound?
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 »

But you do know that. You have the Euro to Pound conversion rate.
Post Reply