views:

59

answers:

2

Hi I need the following php script to do a currency conversion using a different XML file.

Its a script from white-hat design http://www.white-hat-web-design.co.uk/articles/php-currency-conversion.php

The script needs to be amended to do the following:

1, The php script downloads every 24 hours an xml file from rss.timegenie.com/foreign_exchange_rates_forex

rss.timegenie.com/forex.xml or rss.timegenie.com/forex2.xml

2, It then stores the xml file data/contents to a mysql database file ie currency and rate.

Any advice would be appreciated.

A: 

Have a look at simpleXML load-string or load-file. Using this you can parse the XML contents and extract the string. For e.g. (from PHP manuals)

<?php
$string = <<<XML
<?xml version='1.0'?> 
<document>
 <title>Forty What?</title>
 <from>Joe</from>
 <to>Jane</to>
 <body>
  I know that's the answer -- but what's the question?
 </body>
</document>
XML;

$xml = simplexml_load_string($string);

var_dump($xml);
?>

This will output:

SimpleXMLElement Object
(
  [title] => Forty What?
  [from] => Joe
  [to] => Jane
  [body] =>
   I know that's the answer -- but what's the question?
)

Let know how it goes.

pinaki
Thanks im in bit of hurry to do this .. never touched xml before and with limited php experience.I think if i could some how work out how to replace this $pattern = "{<Cube\s*currency='(\w*)'\s*rate='([\d\.]*)'/>}is"; preg_match_all($pattern,$buffer,$xml_rates); array_shift($xml_rates); for($i=0;$i<count($xml_rates[0]);$i++) { $exchange_rate[$xml_rates[0][$i]] = $xml_rates[1][$i]; }With the code you guys are suggesting that would be a great help.Any ideas would be welcomed?
Jack Brown
A: 

The script from white hat design uses a regular expression to extract the currency and rate.

As pinaki mentioned your best bet is to use a more simpler and flexible solution of loading the xml file and parsing it with the simplexml functions. The examples on php.net should get you going. Practice on the following xml and when you can parse this then you you'll easily be able to tackle the real xml from timegenie.com :

<data>
<code>AOA</code>
<description>Angola Kwanza</description>
<rate>125.17</rate>
</data>
zaf