|
1
|
|
|
<?php |
|
2
|
|
|
namespace GJClasses; |
|
3
|
|
|
|
|
4
|
|
|
class Currency |
|
5
|
|
|
{ |
|
6
|
|
|
public $api_key; |
|
7
|
|
|
public $source; |
|
8
|
|
|
|
|
9
|
|
|
public function __construct($source, $api_key) |
|
10
|
|
|
{ |
|
11
|
|
|
$this->source = $source; |
|
12
|
|
|
$this->api_key = $api_key; |
|
13
|
|
|
} |
|
14
|
|
|
|
|
15
|
|
|
public function convertAmount($amount, $from_currency, $to_currency) |
|
16
|
|
|
{ |
|
17
|
|
|
return $this->getConvRate($from_currency, $to_currency) * $amount; |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
public function getConvRate($from_currency, $to_currency) |
|
21
|
|
|
{ |
|
22
|
|
|
if ($from_currency == $to_currency) return 1.0; |
|
23
|
|
|
|
|
24
|
|
|
$conversion_rate = 0.0; |
|
25
|
|
|
|
|
26
|
|
|
if ($this->source === 'era') { |
|
27
|
|
|
|
|
28
|
|
|
$full_url = 'http://api.exchangeratesapi.io/v1/convert?access_key=' . $this->api_key . '&from=' . $from_currency . '&to=' . $to_currency; |
|
29
|
|
|
$remote = new Remote(); |
|
30
|
|
|
$result = $remote->getFileContents($full_url); |
|
31
|
|
|
if ($result === false) return false; |
|
32
|
|
|
$json_result = json_decode($result, true); |
|
|
|
|
|
|
33
|
|
|
$conversion_rate = $json_result['rates'][$to_currency]; |
|
34
|
|
|
|
|
35
|
|
|
} elseif ($this->source === 'er-a') { |
|
36
|
|
|
|
|
37
|
|
|
$full_url = 'https://v6.exchangerate-api.com/v6/' . $this->api_key . '/pair/' . $from_currency . '/' . $to_currency; |
|
38
|
|
|
$remote = new Remote(); |
|
39
|
|
|
$result = $remote->getFileContents($full_url); |
|
40
|
|
|
if ($result === false) return false; |
|
41
|
|
|
$json_result = json_decode($result, true); |
|
42
|
|
|
$conversion_rate = $json_result['conversion_rate']; |
|
43
|
|
|
|
|
44
|
|
|
} elseif ($this->source === 'fixer') { |
|
45
|
|
|
|
|
46
|
|
|
$full_url = 'http://data.fixer.io/api/convert?access_key=' . $this->api_key . '&from=' . $from_currency . '&to=' . $to_currency; |
|
47
|
|
|
$remote = new Remote(); |
|
48
|
|
|
$result = $remote->getFileContents($full_url); |
|
49
|
|
|
if ($result === false) return false; |
|
50
|
|
|
$json_result = json_decode($result, true); |
|
51
|
|
|
$conversion_rate = $json_result['rates'][$to_currency]; |
|
52
|
|
|
|
|
53
|
|
|
} elseif ($this->source === 'interzoid') { |
|
54
|
|
|
|
|
55
|
|
|
$full_url = 'https://api.interzoid.com/convertcurrency?license=' . $this->api_key . '&from=' . $from_currency . '&to=' . $to_currency . '&amount=1'; |
|
56
|
|
|
$remote = new Remote(); |
|
57
|
|
|
$result = $remote->getFileContents($full_url); |
|
58
|
|
|
if ($result === false) return false; |
|
59
|
|
|
$json_result = json_decode($result, true); |
|
60
|
|
|
$conversion_rate = $json_result['Converted']; |
|
61
|
|
|
|
|
62
|
|
|
} elseif ($this->source === 'erh') { |
|
63
|
|
|
|
|
64
|
|
|
$full_url = 'https://api.exchangerate.host/convert?from=' . $from_currency . '&to=' . $to_currency; |
|
65
|
|
|
$remote = new Remote(); |
|
66
|
|
|
$result = $remote->getFileContents($full_url); |
|
67
|
|
|
if ($result === false) return false; |
|
68
|
|
|
$json_result = json_decode($result, true); |
|
69
|
|
|
$conversion_rate = $json_result['info']['rate']; |
|
70
|
|
|
|
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
return $conversion_rate; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|