PHP - Currency Conversion

Posted on by Kenny Cason
tags = [ currency conversion, php ]

I can’t remember when I wrote this but I found this little snippet in my library and didn’t want it to go to waste.

It’s very simple and just uses Yahoo! Finance to get the quote. To use the Class simply use the below code:

1
2
  $currency = new Currency();
  print_r($currency->convert('USD', 'JPY', 100));

It should then echo out something like:

1
Array ( [currency] => USDJPY [rate] => 85.995 [date] => 12/28/2012 [time] => 5:55pm [value] => 85.995 )

I.e. 100 USD is now worth $85.995 in Japan, this is called 円高(endaka) in Japanese :)

Currency.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?php

class Currency {

    public function __construct() {

    }

    public function convert($from='USD', $to='GBP', $amount=null) {

        $url = 'http://finance.yahoo.com/d/quotes.csv?e=.csv&f=sl1d1t1&s=' . $from . $to . '=X';
        $handle = @fopen($url, 'r');
        if ($handle) {
            $result = fgets($handle, 4096);
            fclose($handle);
        }
        $result = preg_replace('/"/', '', $result);
        $array = explode(',', $result);

        $ret = array('currency' => substr($array[0], 0, strlen($array[0]) - 2),
                    'rate' => $array[1],
                    'date' => $array[2],
                    'time' => $array[3]);
        if($amount != null) {
            $ret['value'] = $amount * ($array[1] / 100.0);
        }
        return $ret;
    }

}

Usage

1
2
$currency = new Currency();
print_r($currency->convert('USD', 'JPY', 100));
comments powered by Disqus