Martine et Olivier ALLAIS Chambres d'hôtes Corbières

Chambres et table d'hôtes dans les Hautes Corbières - Aude

Accueil Chambres Hotes

Obtenir le taux de change d'une devise face à l'Euro ( script PHP )

Francais  

Les Jourdets

Chambres d'hôtes
Table d'hôtes

Martine & Olivier ALLAIS

Les Jourdets
11330 Lairière, France

mail
Email
mallais@free.fr

phone
Tel GSM
04 68 70 04 76 06 08 33 60 32

Hautes Corbières - Aude

les Jourdets

Pour obtenir ces taux de change en direct, il suffit d'aller sur une page les affichant, le lire le contenu de la page , et d'extraire les infos intéressantes.

A titre d'exemple voici un petit script qui va extraire plusieurs taux d'un fichier XML généré par la banque centrale européenne et donc peu succeptible de changer de format trop souvent : http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml

Code du script

<?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('UTF-8');
    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 : 


$refprice = 31;
if ( isset($Rates['GBP']) )  
		echo '<p class="notaprix" >At today\'s exchange rates, '.$refprice.' € = '.round($refprice*$Rates['GBP'],2).' GBP</p>';     	 
?>
Retour aux codes