Obtenir les taux de change du jour en euros

Contenu du snippet

Un petit bout de code php tiré d'un forum et adapté au fichier xml mis à jour quotidiennement par euronext. On peut aller chercher cette info sur plein de pages mais on peut espérer que celle ci ne change pas trop souvent ...

Source / Exemple :


<?php
//  Set the base URI.
$URI = 'http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml';

//  Make sure there's no other data with these names.

$Rates = array();
$DataProbs   = array();

//  Array to convert XML entities back to plain text.
$XmlEntities = array(
    '&amp;'  => '&',
    '<'   => '<',
    '>'   => '>',
    '&apos;' => '\'',
    '&quot;' => '"',
);

/**

  • Runs each time an XML element starts.
  • /
function StartHandler(&$Parser, &$Elem, &$Attr) { global $Data, $CData, $XmlEntities; // Start with empty CData array. $CData = array(); // Put each attribute into the Data array. foreach ($Attr as $Key => $Value) { $Data["$Elem:$Key"] = strtr(trim($Value), $XmlEntities); } } /**
  • Runs each time XML character data is encountered.
  • /
function CharacterHandler(&$Parser, &$Line) { global $CData; /*
  • Place lines into an array because elements
  • can contain more than one line of data.
  • /
$CData[] = $Line; } /**
  • Runs each time an XML element ends.
  • /
function EndHandler(&$Parser, &$Elem) { global $Data, $CData, $DataProbs, $Sym, $XmlEntities,$Rates; /*
  • Mush all of the CData lines into a string
  • and put it into the $Data array.
  • /
$Data[$Elem] = strtr( trim( implode('', $CData) ), $XmlEntities); switch ($Elem) { case 'CUBE': if ( isset($Data['CUBE:CURRENCY']) && isset($Data['CUBE:RATE']) ) { $Rates[$Data['CUBE:CURRENCY']] = (float)$Data['CUBE:RATE']; } break; } } // Get the file ... /*
  • Grab the file and stick it into an array.
  • Next, check to see that you actually got the raw info.
  • Then, implode the raw info into one long string.
*
  • If your data is already in string form, you don't need these steps.
*
  • This one step requires PHP to be at version 4.3.0 or later.
  • /
$Contents = @file_get_contents("$URI"); if ( $Contents ) { $Data = array(); // Initialize the parser. $Parser = xml_parser_create('ISO-8859-1'); xml_set_element_handler($Parser, 'StartHandler', 'EndHandler'); xml_set_character_data_handler($Parser, 'CharacterHandler'); // Pass the content string to the parser. if ( !xml_parse($Parser, $Contents, TRUE) ) { // problem ( silence ! ) } } // ce script est volontairement silencieux sur les ereurs // exemple pour l'utiliser : /* require_once("taux.php"); if ( isset($Rates['GBP']) ) echo '<p class="notaprix" >At today\'s exchange rates, '.$refprice.' € = '.round($refprice*$Rates['GBP'],2).' GBP</p>';
  • /
  • ?>

Conclusion :


On pet simplifier largement mais j'aime bien l'exemple SAX

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.