Page 1 of 2

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

Posted: Mon Sep 19, 2016 5:16 am
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?

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

Posted: Mon Sep 19, 2016 6:47 am
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

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

Posted: Mon Sep 19, 2016 6:55 am
by simonmlewis
I've no idea how to get into XML and export to a PHP variable. Can you please advise?

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

Posted: Mon Sep 19, 2016 6:58 am
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.

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

Posted: Mon Sep 19, 2016 7:07 am
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.

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

Posted: Mon Sep 19, 2016 7:17 am
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;

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

Posted: Mon Sep 19, 2016 7:25 am
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;

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

Posted: Mon Sep 19, 2016 7:30 am
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.

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

Posted: Mon Sep 19, 2016 7:32 am
by simonmlewis
My local version doesn't like:

Code: Select all

$eurofind = new SimpleXMLElement($xmleuro);

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

Posted: Mon Sep 19, 2016 8:48 am
by Celauran
simonmlewis wrote:My local version doesn't like:

Code: Select all

$eurofind = new SimpleXMLElement($xmleuro);
What does that even mean?

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

Posted: Mon Sep 19, 2016 8:49 am
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'];

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

Posted: Mon Sep 19, 2016 9:03 am
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.

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

Posted: Mon Sep 19, 2016 9:10 am
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.

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

Posted: Mon Sep 19, 2016 9:16 am
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?

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

Posted: Mon Sep 19, 2016 9:26 am
by Celauran
But you do know that. You have the Euro to Pound conversion rate.