ecb-exchange-rates.php 934 B

123456789101112131415161718192021222324
  1. <?php
  2. $cacheFile = '/tmp/numbat-exchange-rates-cache.xml';
  3. $cacheTime = 24 * 60 * 60; // 24 hours in seconds
  4. $ecbUrl = 'https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml';
  5. if (file_exists($cacheFile) && (time() - filemtime($cacheFile) < $cacheTime)) {
  6. // If cache file is less than 24 hours old, serve it
  7. header('Content-Type: text/xml');
  8. echo file_get_contents($cacheFile);
  9. } else {
  10. // Else, fetch fresh data and cache it
  11. $xmlData = file_get_contents($ecbUrl);
  12. if ($xmlData) {
  13. file_put_contents($cacheFile, $xmlData); // Cache the fetched data
  14. header('Content-Type: text/xml');
  15. echo $xmlData; // Serve the fetched data
  16. } else {
  17. // Handle failure in fetching data (e.g., serve cached data even if older than 24 hours or serve an error message)
  18. header('HTTP/1.1 500 Internal Server Error');
  19. echo "Error fetching the exchange rates.";
  20. }
  21. }
  22. ?>