Question : Convert XML to PHP Feed

Hi,

I'm quite new to PHP and XML so please explain in simple terms when answering.

I want to use the following XML Feed http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml and convert the data into php.

I want to be able to make a call such as ?currency=USD and then my variables will be set as:

$currency='rate'

Could someone show me how i can do this in PHP?

Many thanks!

Answer : Convert XML to PHP Feed

You could use http://www.php.net/simplexml to load the XML and then process it into variables. The snippet below simply outputs it to screen, but you could equally well store it in a PHP array. The snippet currently produces output like this....

USD - 1.2704
JPY - 108.23
BGN - 1.9558

etc
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
<?php

$feed = "http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml";

// Fetch the XML
//
$xml = simplexml_load_file( $feed );

// Process the currencies
//
foreach ( $xml->Cube->Cube->Cube as $anEntry ) {

     // The values for each entry are stored in the attributes
     // so pull these to an array
     //
     $attrib = $anEntry->attributes();

     // Convert to variables for easier handling
     //
     $currencyName = $attrib['currency'];
     $currencyRate = $attrib['rate'];

     // output to screen
     //
     echo "$currencyName - $currencyRate<br/>";
}
Random Solutions  
 
programming4us programming4us