begin process at 2012 05 27 19:31:35
  Trouver un code source :
 
dans
 
Accueil > 

Code

 > 

Xml

 > OBTENIR LES TAUX DE CHANGE DU JOUR EN EUROS

OBTENIR LES TAUX DE CHANGE DU JOUR EN EUROS


 Information sur la source

 Description

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

  • <?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;' => '&',
  • '&lt;' => '<',
  • '&gt;' => '>',
  • '&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>';
  • */
  • *?>
<?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;'  => '&',
    '&lt;'   => '<',
    '&gt;'   => '>',
    '&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


 Sources du même auteur

Source avec Zip Source avec une capture CALENDRIER RÉSERVATION POUR CHAMBRES D'HÔTES EN PHP MYSQL

 Sources de la même categorie

Source avec Zip JEU FRISE CHRONOLOGIQUE EN XML par mldvb
Source avec Zip AFFICHER LES FILM EN SALLE par slhuilli
Source avec Zip Source avec une capture MINI-PROCESSEUR XPROC (PIPELINE XML) par ordiman85
Source avec Zip Source avec une capture XML MAPPING TO CLASS OBJECTS / CHARGEMENT / PARSING / MODIFI... par aKheNathOn
Source avec Zip Source avec une capture VIEWER POUR JALBUM SKIN CHAMELEON LIVRE D'OR par ym_trainz

 Sources en rapport avec celle ci

Source avec Zip TRANSFORMER UNE DATE FORMAT ANGLOPHONE( AAAA-MM-JJ) EN FORMA... par italiasky

Commentaires et avis

Commentaire de elcoyotos le 16/12/2011 15:06:27

La même chose en deux lignes de code ^^

$XML=simplexml_load_file("http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml");
foreach($XML->Cube->Cube->Cube as $rate){ echo '1&euro;='.$rate["rate"].' '.$rate["currency"].'<br/>'; }

 Ajouter un commentaire


Discussions en rapport avec ce code source dans le forum

onChange, javascript probleme de lien avec des parametres [ par pcpourtous ] Bonjour, voila :echo"&lt;SCRIPT LANGUAGE=\"Javascript\"&gt;";echo"function on_change(){ window.location.href = \"stock.php?agence=$agence&type=.$type. Session, id qui change [ par perig ] Bonjours Encore une petite questionSur mes pages j'utilise une session mais elle est instable, des fois elles ne fonctionne pas.Pourtant c'est un code JS ET PHP HELP SVP [ par rafou77 ] Bonjour et merci de votre attention,J'ai un fichier js avec des taux qui changent en fonction d'un select dans une page php, j'aimerai pourvoir change UPDATE BASE AVEC CHAMPS PAR RAPPORT A JAVASCRIPT [ par rafou77 ] Bonjour, voilà j'ai une page qui s'appel taux2.php ou là il ya des champs avec des taux dans une base que je peux mettre à jour sans prob... juste en taux de doublons [ par semouna ] bonsoir j'aimerai calculer le taux de doublons d'une table en passant par une requète sous accessmerci "function change "qui change rien [ par peterbud ] [j'utilise easy php1.7 sous windows Xp -je suis très grand débutant] BONJOUR à tous,alors voilà,je tape le code suivant:&lt;?phpfunct Comment récupérer le taux de conversion d'une devise [ par Biboune ] Bonjour,Voil&#224; mon probl&#232;me :J'essaie de r&#233;cup&#233;rer le r&#233;sultat de la conversion d'une devise en appelant la page suivante : ht la valeur d'une variable avant un submit [ par sunshine2004 ] bonjour voilaj'ai 2liste deroulante qui son aliment&#233;&nbsp; a partir d'une bdd c'est deux liste sont li&#233;equand je change de region l'evenemen Comment faire pour qu'une case de type input text change de couleur [ par rich25200 ] Bonjour, Je sais pas qi je suis dans le bon forum mais j'ai pas trouver de forum CSS ou autre, mais j'aimerai savoir comment faire pour qu'un chanp d Question sur les dates? [ par Monico9385 ] Bonjour, j'ai une petite question concernant les dates. J'aimerai savoir comment s'est possible de savoir quand on change d'ann&#233;e. Je m'explique


Nos sponsors


Sondage...

Comparez les prix

CalendriCode

Mai 2012
LMMJVSD
 123456
78910111213
14151617181920
21222324252627
28293031   

Consulter la suite du CalendriCode

A découvrir



 
Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel (EBArtSoft), Merci à Vincent pour ses précieux conseils.
CodeS-SourceS.com© Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
CodeS-SourceS.com© est une marque déposée tous droits réservés

Google Coop CodeS-SourceS Google Coop CodeS-SourceS
Temps d'éxécution de la page : 0,733 sec (3)

Nous contacter | Annoncer sur CodeS-SourceS | Mentions légales