Thanks for the reference.
Is there another way to do it rather than using regex.
I am right in thinking this just parses the XML line by line.
Is there a way to acces the nodes anotehr way?
<?php
//This is a PHP (4/5) script example on how eurofxref-daily.xml can be parsed
//Read eurofxref-daily.xml file in memory
$XMLContent= file("http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml");
//the file is updated daily between 2.15 p.m. and 3.00 p.m. CET
foreach ($XMLContent as $line) {
if (ereg("currency='([[:alpha:]]+)'",$line,$currencyCode)) {
if (ereg("rate='([[:graph:]]+)'",$line,$rate)) {
//Output the value of 1 EUR for a currency code
echo '1 € = '.$rate[1].' '.$currencyCode[1].'<br />';
//--------------------------------------------------
// Here you can add your code for inserting
// $rate[1] and $currencyCode[1] into your database
//--------------------------------------------------
}
}
}
?>
Output of the code above
1 € = 1.4064 USD
1 € = 129.14 JPY
1 € = 1.9558 BGN
1 € = 25.979 CZK
1 € = 7.4426 DKK
1 € = 15.6466 EEK
1 € = 0.87000 GBP
1 € = 270.43 HUF
1 € = 3.4528 LTL
1 € = 0.7088 LVL
1 € = 4.0545 PLN
1 € = 4.1275 RON
1 € = 10.1375 SEK
1 € = 1.4723 CHF
1 € = 8.1455 NOK
1 € = 7.2977 HRK
1 € = 41.8618 RUB
1 € = 2.0778 TRY
1 € = 1.5463 AUD
1 € = 2.5117 BRL
1 € = 1.4740 CAD
1 € = 9.6012 CNY
1 € = 10.9265 HKD
1 € = 13127.00 IDR
1 € = 64.7500 INR
1 € = 1599.54 KRW
1 € = 17.8760 MXN
1 € = 4.7501 MYR
1 € = 1.9590 NZD
1 € = 64.685 PHP
1 € = 1.9725 SGD
1 € = 46.418 THB
1 € = 10.5954 ZAR
The provided code does that, but it’s trivial to place the currencies that you’re interested in into an array, and then check if the ones that are found are contained in there.
$currencyToFind = array('USD', 'GBP');
$currency = array();
...
if (in_array($currencyCode, $currencyToFind)) {
$currency[$currencyCode] = $rate;
}
There certainly are, and they can be further explored once you have a working solution.
Get it working first, and then improve upon it.
And if I wanted to get the time of the XML?
RegExp, really? For an XML document?
<?php
$xml = new SimpleXMLElement(
'http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml',
null,
true
);
printf('XML received at %s', $xml->Cube->Cube['time']);
foreach($xml->Cube->Cube as $entry){
echo $entry['currency'], ' = ', $entry['rate'];
}
?>
That’s what I was looking for.
Nice one Mr Sterling!
What would I need to do if I wanted to pluck out specific currencies from the feed namely, USD and GBP?
That has already been provided, in post #4
i couldn’t be any more ignorant if i tried. sorry
What then do you currently have.
I currently have this;
<?php
$xml = new SimpleXMLElement(
'http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml',
null,
true
);
printf('XML received at %s', $xml->Cube->Cube['time']."<br>");
foreach($xml->Cube->Cube->Cube as $entry){
echo $entry['currency'], ' = ', $entry['rate']."<br>";
}
?>
I want to select a few specific currencies
$currencyToFind = array('USD', 'GBP');
$currency = array();
...
if (in_array($currencyCode, $currencyToFind)) {
$currency[$currencyCode] = $rate;
}
…but I’m not quite sure how to get there.
I’ll make a small adjustment to the following code to account for how your code is structured:
$currencyToFind = array('USD', 'GBP');
$currency = array();
...
$currencyCode = $entry['currency'];
if (in_array($currencyCode, $currencyToFind)) {
$currency[$currencyCode] = $entry['rate'];
}
Place the first two lines before the foreach loop, then use the rest of the code to replace the echo line inside of the foreach statement.
Afterwards, you will have an array called $currency that contains two values, indexed by ‘USD’ and ‘GDP’
In other words, $currency[‘USD’] will contain a value, and $currency[‘GBP’] will contain another value.
Just add the required currency names in the $rates array as per the example.
<?php
$xml = new SimpleXMLElement(
'http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml',
null,
true
);
printf('XML received at %s <br />', $xml->Cube->Cube['time']);
$rates = array(
'GBP' => null,
'USD' => null
);
foreach($xml->Cube->Cube->Cube as $item){
if(true === array_key_exists((string)$item['currency'], $rates)){
$rates[(string)$item['currency']] = (float)$item['rate'];
}
}
print_r($rates);
/*
Array
(
[GBP] => 0.8733
[USD] => 1.4085
)
*/
?>
Edit: Bah, beaten to it by pwm57. Again.
Off Topic:
XPath would be great for this, but some reason, despite my many amateur attempts at it, it just won’t work. Maybe due to the nested Cube/Cube/Cube nonsense?
AnthonySterling:
Off Topic:
XPath would be great for this, but some reason, despite my many amateur attempts at it, it just won’t work. Maybe due to the nested Cube/Cube/Cube nonsense?
<?php
$xml = <<<EOXML
...
EOXML;
$dom = new SimpleXMLElement( $xml );
$part = $dom->xpath( './/*[@currency]' );
foreach ( $part as $val )
echo $val['currency'], ' = ', $val['rate'], "\
";
OK thanks for all the help.
There are a few things I have to learn here.
What, no conditionals Logic_Earth?
<?php
$xml = new SimpleXMLElement(
'http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml',
null,
true
);
printf('Data valid at %s <br />', date('r', strtotime((string)$xml->Cube->Cube['time'])));
foreach($xml->xpath(".//*[@currency='USD' or @currency='GBP']") as $item){
printf('%s = %f<br />', $item['currency'], $item['rate']);
}
/*
Data valid at Wed, 27 Jan 2010 00:00:00 +0000
USD = 1.407200
GBP = 0.867250
*/
?>