Currency exchange online

Asked

Viewed 3,548 times

6

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

(Eventually I found a solution that I like that I put here.)

  • Can some solution also contemplate the parallel? :)

  • @bigown, what is a parallel?

  • I forgot the context. In Brazil there is black market quotation. It’s an unofficial street quote, usually used in non-state... approved operations (let’s say so) by the government. It’s used in many situations. Here is a rate known as Ptax (daily average of quotations) too, which is official. There are other situations where the calculation is not daily either. It is more complicated to get an unofficial quote automatically although it is very useful. You’ll get the quote from who?

2 answers

7

Another option is to use data from european central bank.

They make the data available at this url (daily updated).

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

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

  • Rate of the real (BRL): 3.2091
  • US dollar rate (USD): 1.3776
  • Value of one US dollar in real: 3,2091 / 1,3776 = 2.33

Example of XML 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 to use in PHP:

<?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);
?>

6


Here’s a hint 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

  • I marked this answer as "accepted" for having customer side solution examples.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.