Currency exchange online

6

I spent some time looking for a site where I can change currency. Where can I make requests via client-side script using a url address and receive a JSON object with the conversion.

(I ended up finding a solution I like that I put here.)

    
asked by anonymous 17.12.2013 / 00:17

2 answers

6

Here's a suggestion using http://rate-exchange.appspot.com

Using jQuery

var fazerRequest = function (valor, origem, destino) {
    $.ajax({
        type: "GET",
        url: "http://rate-exchange.appspot.com/currency?from=" +
             origem + "&to=" + destino + "&q=" + valor,
        dataType: "jsonp",
        success: function (response) {
            $('#resultado').html(response.v + ' ' + destino);
        }
    });
}

Example

Using Mootools

Request.exchange = new Class({
    Extends: Request.JSONP,
    options: {
        url: 'http://rate-exchange.appspot.com/currency?from={from}&to={to}&q={amount}',
        amount: 1
    },
    initialize: function (options) {
        this.setOptions(options);
        this.options.url = this.options.url.substitute(this.options);
        this.parent();
    }
});

var fazerRequest = function (valor, origem, destino) {
    new Request.exchange({
        from: origem,
        to: destino,
        amount: valor,
        onSuccess: function (response) {
            document.id('resultado').set('html', response.v + ' ' + destino);
        }
    }).send();
}

Mootools version inspired by Dimitar

Example

    
17.12.2013 / 00:17
7

Another option is to use the data from the European Central Bank .

They make the data in this url (updated daily).

The rate (attribute rate ) is based on the value of the euro. The euro is omitted from the list, but if it were your rate would be worth 1.

To perform the conversion simply divide one rate by the other. For example:

  • Real rate (BRL): 3.2091
  • US Dollar Rate (USD): 1.3776
  • Value of one US dollar in reais: 3.2091 / 1.3776 = 2.33

XML sample returned:

<?xml version="1.0" encoding="UTF-8"?>
<gesmes:Envelope xmlns:gesmes="http://www.gesmes.org/xml/2002-08-01" xmlns="http://www.ecb.int/vocabulary/2002-08-01/eurofxref">
    <gesmes:subject>Reference rates</gesmes:subject>
    <gesmes:Sender>
        <gesmes:name>European Central Bank</gesmes:name>
    </gesmes:Sender>
    <Cube>
        <Cube time="2013-12-16">
            <Cube currency="USD" rate="1.3776" />
            <Cube currency="JPY" rate="141.87" />
            <Cube currency="BGN" rate="1.9558" />
            <Cube currency="CZK" rate="27.600" />
            <Cube currency="DKK" rate="7.4605" />
            <Cube currency="GBP" rate="0.84385" />
            <Cube currency="HUF" rate="299.77" />
            <Cube currency="LTL" rate="3.4528" />
            <Cube currency="LVL" rate="0.7029" />
            <Cube currency="PLN" rate="4.1758" />
            <Cube currency="RON" rate="4.4570" />
            <Cube currency="SEK" rate="9.0266" />
            <Cube currency="CHF" rate="1.2212" />
            <Cube currency="NOK" rate="8.4345" />
            <Cube currency="HRK" rate="7.6293" />
            <Cube currency="RUB" rate="45.2905" />
            <Cube currency="TRY" rate="2.7970" />
            <Cube currency="AUD" rate="1.5400" />
            <Cube currency="BRL" rate="3.2091" />
            <Cube currency="CAD" rate="1.4579" />
            <Cube currency="CNY" rate="8.3651" />
            <Cube currency="HKD" rate="10.6815" />
            <Cube currency="IDR" rate="16507.61" />
            <Cube currency="ILS" rate="4.8281" />
            <Cube currency="INR" rate="85.0670" />
            <Cube currency="KRW" rate="1450.66" />
            <Cube currency="MXN" rate="17.8013" />
            <Cube currency="MYR" rate="4.4626" />
            <Cube currency="NZD" rate="1.6673" />
            <Cube currency="PHP" rate="60.700" />
            <Cube currency="SGD" rate="1.7293" />
            <Cube currency="THB" rate="44.125" />
            <Cube currency="ZAR" rate="14.2072" />
        </Cube>
    </Cube>
</gesmes:Envelope>

They also teach you how to use in PHP : / p>

<?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);
?>
    
17.12.2013 / 02:04