-5

I was thinking is there any easy solution to convert euro prices to dollars with PHP. Something similar to date and time function maybe.

edit. I have euro prices on the site but I'd like to have dollar prices along them too. So I need solution to auto calc dollars in real time.

Izir Atig
  • 33
  • 2
  • 10

2 Answers2

2

You will need to use a 3-rd party api to do this conversion for you. Please take a look at this one, for example: http://fixer.io/ . Not to reinvent the wheel, I recommend you using a ready-made wrapper for actually making the requests. I've found this one which looks pretty thin and solid: https://github.com/fadion/Fixerio

Then, all you have to do is something like that:

$exchange = new Exchange();
$exchange->base(Currency::USD);
$exchange->symbols(Currency::EUR, Currency::GBP);

$rates = $exchange->get();
Alex
  • 187
  • 4
1

A good code snippet is to use the Foreign Exchganges API In order to f the job. A sample code is the following:

    function currencyConverter($from, $to, $amount)
    {
        if ($from != $to) {
            $json = getJSONetP('https://api.exchangeratesapi.io/latest?base=' . strtoupper($from));
            return intval($json['rates'][$to] * $amount);
        }
        return $amount;
    }

The getJSONetP is a fuction used to receive reponses in jsonp format.

Dimitrios Desyllas
  • 9,082
  • 15
  • 74
  • 164
Juan Denis
  • 21
  • 7
  • 1
    Please provide detail about your answer. – Skoua Jun 20 '19 at 17:02
  • While this code may solve the question, [including an explanation](https://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanation, and give an indication of what limitations and assumptions apply. – Dave Jun 20 '19 at 17:16