Rev 16 | Blame | Compare with Previous | Last modification | View Log | RSS feed
<?php$rateFile = "exchangeRates.php";if (is_file($rateFile)) {$diff = (time() - filemtime($rateFile)) / 86400;echo "$rateFile last modified " . $diff . " days ago." . PHP_EOL;if ($diff < 0.9) {echo "Exiting - File too new." . PHP_EOL;exit(1);}}$str = '<?php' . PHP_EOL;$str .= 'function getExchangeRate($source, $target) {' . PHP_EOL;$str .= ' $currencyRates = array (' . PHP_EOL;/*$str .= retrieveRates("AUD");$str .= ','. PHP_EOL;$str .= retrieveRates("CAD");$str .= ','. PHP_EOL;$str .= retrieveRates("CNY");$str .= ','. PHP_EOL;$str .= retrieveRates("EUR");$str .= ','. PHP_EOL;$str .= retrieveRates("GBP");$str .= ','. PHP_EOL;$str .= retrieveRates("HKD");$str .= ','. PHP_EOL;$str .= retrieveRates("INR");$str .= ','. PHP_EOL;*/$str .= retrieveRates("USD");$str .= ' );' . PHP_EOL;$str .= '' . PHP_EOL;$str .= ' $rate = 1;' . PHP_EOL;$str .= '' . PHP_EOL;$str .= ' if (array_key_exists($source, $currencyRates)) {' . PHP_EOL;$str .= ' if (array_key_exists($target, $currencyRates[$source])) {' . PHP_EOL;$str .= ' $rate = $currencyRates[$source][$target];' . PHP_EOL;$str .= ' }' . PHP_EOL;$str .= ' }' . PHP_EOL;$str .= '' . PHP_EOL;$str .= ' return($rate);' . PHP_EOL;$str .= '}' . PHP_EOL;$str .= '?>' . PHP_EOL;if (is_file($rateFile)) {rename($rateFile, $rateFile . ".old");}file_put_contents($rateFile, $str);echo "$rateFile updated." . PHP_EOL;exit(0);function retrieveRates($source) {$accessKey = "9ca495b56281a691775f8b289e586f8f";$apiCall = "http://apilayer.net/api/live?access_key=" . $accessKey . "&source=";$json = file_get_contents($apiCall . $source);$json = json_decode($json);if ($json->success !== true) {echo "CurrencyLayer Error (" . $source . "): " . $json->error->code . " / " . $json->error->info . PHP_EOL;echo "Exiting." . PHP_EOL;exit(2);}$str = ' "' . $source . '" => array (' . PHP_EOL;foreach ($json->quotes as $symbol => $quote) {$str .= ' "' . substr($symbol, 3) . '" => ' . $quote . ',' . PHP_EOL;}$str .= ' )' . PHP_EOL;return ($str);}?>