0

I have a new website that i want the exchange rates to show up using the ECB (European Central Bank)
Now for some reason on their website they put this code but i copy paste it into file save as php upload to server and nothing ... what could be the issue spent the whol day now racking my brain around this XML

Regular expression example

<?php
    //This is a PHP(4/5) script example on how eurofxref-daily.xml can be parsed
    //Read eurofxref-daily.xml file in memory 
    //For this command you will need the config option allow_url_fopen=On (default)
    $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(preg_match("/currency='([[:alpha:]]+)'/",$line,$currencyCode)){
            if(preg_match("/rate='([[:graph:]]+)'/",$line,$rate)){
                //Output the value of 1EUR for a currency code
                echo'1&euro;='.$rate[1].' '.$currencyCode[1].'<br/>';
                //--------------------------------------------------
                //Here you can add your code for inserting
                //$rate[1] and $currencyCode[1] into your database
                //--------------------------------------------------
            }
        }
}
?>

XML parser example

<?php
    function StartElement($parser, $name, $attrs) { 
        if (!empty($attrs['RATE'])) {
            echo "1&euro;=".$attrs['RATE']." ".$attrs['CURRENCY']."<br />"; 
        }
    }
    $xml_parser= xml_parser_create();
    xml_set_element_handler($xml_parser, "StartElement", "");
    // for the following command you will need file_get_contents (PHP >= 4.3.0) 
    // and the config option allow_url_fopen=On (default)
    xml_parse($xml_parser, file_get_contents ("http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml"));
    xml_parser_free($xml_parser);
?>

SimpleXML example

<?php
    //This is aPHP(5)script example on how eurofxref-daily.xml can be parsed
    //Read eurofxref-daily.xml file in memory
    //For the next command you will need the config option allow_url_fopen=On (default)
    $XML=simplexml_load_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($XML->Cube->Cube->Cube as $rate){
        //Output the value of 1EUR for a currency code
        echo '1&euro;='.$rate["rate"].' '.$rate["currency"].'<br/>';
        //--------------------------------------------------
        //Here you can add your code for inserting
        //$rate["rate"] and $rate["currency"] into your database
        //--------------------------------------------------
    }
?>

This code is from their own website Click Here

2
Contributors
2
Replies
14
Views
3 Years
Discussion Span
Last Post by branding4you
0

You can use attributes(), for example:

<?php

$file = simplexml_load_file("http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml");

foreach($file as $key => $value)
{
    echo "Time: ". $value->Cube->attributes()->time . "<br /><br />";
    foreach($value->Cube->Cube as $key_1 => $value_1)
    {
        echo "Currency: ". $value_1->attributes()->currency . " Rate:". $value_1->attributes()->rate ."<br />";
    }
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.