Rev 21 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 21 | Rev 22 | ||
---|---|---|---|
Line 1... | Line 1... | ||
1 | <?php |
1 | <?php |
2 | 2 | ||
3 | // PHP library for CurrencyLayer |
3 | // PHP library for CurrencyLayer |
4 | // (C) 2017 ViaThinkSoft, Daniel Marschall |
4 | // (C) 2017-2022 ViaThinkSoft, Daniel Marschall |
5 | // Revision: 2017-03-25 |
5 | // Revision: 2022-08-06 |
6 | // |
6 | // |
7 | // More information at https://currencylayer.com/documentation |
7 | // More information at https://currencylayer.com/documentation |
8 | 8 | ||
9 | # EXAMPLE |
9 | # EXAMPLE |
10 | /* |
10 | /* |
Line 110... | Line 110... | ||
110 | $this->check_json_file_exists(); |
110 | $this->check_json_file_exists(); |
111 | $data = json_decode(file_get_contents($this->jsonfile), true); |
111 | $data = json_decode(file_get_contents($this->jsonfile), true); |
112 | $this->check_data_ok($data); |
112 | $this->check_data_ok($data); |
113 | 113 | ||
114 | $exchange_rates_ary = array(); |
114 | $exchange_rates_ary = array(); |
- | 115 | $source = $data['source']; |
|
115 | $quotes = $data['quotes']; |
116 | $quotes = $data['quotes']; |
- | 117 | $quotes['USDUSD'] = 1; // missing 06 Aug 2022. A bug?? Reported https://github.com/apilayer/currencylayer-API/issues/16 |
|
116 | foreach ($quotes as $n => $v) { |
118 | foreach ($quotes as $n => $v) { |
- | 119 | if ($source == substr($n, 0, 3)) { |
|
117 | $exchange_rates_ary[substr($n, 3, 3)] = $v; // key: USDxxx=12.345 |
120 | $exchange_rates_ary[substr($n, 3, 3)] = $v; // key: USDxxx=12.345 |
118 | } |
121 | } |
- | 122 | } |
|
119 | unset($quotes); |
123 | unset($quotes); |
120 | 124 | ||
- | 125 | asort($exchange_rates_ary); |
|
121 | $this->cache_exchange_rates_ary = $exchange_rates_ary; |
126 | $this->cache_exchange_rates_ary = $exchange_rates_ary; |
122 | return $exchange_rates_ary; |
127 | return $exchange_rates_ary; |
123 | } |
128 | } |
124 | 129 | ||
125 | public function get_supported_currencies() { |
130 | public function get_supported_currencies() { |
Line 132... | Line 137... | ||
132 | 137 | ||
133 | if ($from_cur == $to_cur) return $value; |
138 | if ($from_cur == $to_cur) return $value; |
134 | 139 | ||
135 | $exchange_rates_ary = $this->get_exchange_rates_ary(); |
140 | $exchange_rates_ary = $this->get_exchange_rates_ary(); |
136 | 141 | ||
137 | if (!isset($exchange_rates_ary[$from_cur])) throw new CurCalcException('Source curreny $from_cur not found in exchange data.'); |
142 | if (!isset($exchange_rates_ary[$from_cur])) throw new CurCalcException("Source curreny $from_cur not found in exchange data."); |
138 | if (!isset($exchange_rates_ary[$to_cur])) throw new CurCalcException('Destination curreny $to_cur not found in exchange data.'); |
143 | if (!isset($exchange_rates_ary[$to_cur])) throw new CurCalcException("Destination curreny $to_cur not found in exchange data."); |
139 | 144 | ||
140 | return $value * $exchange_rates_ary[$to_cur]/$exchange_rates_ary[$from_cur]; |
145 | return $value * $exchange_rates_ary[$to_cur]/$exchange_rates_ary[$from_cur]; |
141 | } |
146 | } |
142 | 147 | ||
143 | } |
148 | } |