1. Go to this page and download the library: Download planetbiru/forex library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
planetbiru / forex example snippets
use Planetbiru\Forex\Forex;
// Example 1: Initialization without loading initial data
$forex = new Forex();
$forex->loadDaily(); // Manually load data afterwards
// Example 2: Direct initialization with daily data
$forex = new Forex(Forex::ECB_DAILY);
// Example 3: Direct initialization with last 90 days data
$forex = new Forex(Forex::ECB_90DAYS);
$forex = new Forex();
$forex->loadDaily();
echo "Daily data loaded. Date: " . $forex->getDate();
$forex = new Forex();
// ... perform other operations ...
$forex->loadDaily(); // Refresh to the latest daily data
$forex = new Forex();
$forex->loadDaily();
if ($forex->getDate() == date('Y-m-d')) {
echo "Today's data is already available.";
}
$forex = new Forex();
$forex->loadLast90Days();
$forex = new Forex();
$forex->load(Forex::ECB_90DAYS); // Equivalent to loadLast90Days()
// Typically, loadHistory is not called directly when searching for a specific date,
// but this file is used internally by loadByDate.
$forex = new Forex();
$forex->loadHistory(); // Loads the most recent data from the large history file
$forex = new Forex();
// Caution: this loads a relatively large XML file
$forex->loadHistory();
echo "Number of available currencies: " . count($forex->getAllRates());
$forex = new Forex();
$forex->loadByDate('2024-01-03'); // Ensure the date is not a holiday/weekend
echo "USD rate on 2024-01-03: " . $forex->get('USD');
$forex = new Forex();
try {
$forex->loadByDate('2025-01-01'); // Future date
} catch (Exception $e) {
echo "Data not found: " . $e->getMessage();
}
$forex = new Forex();
$dates = ['2023-05-01', '2023-05-02', '2023-05-03'];
foreach ($dates as $date) {
try {
$forex->loadByDate($date);
echo "EUR to USD rate on $date: " . $forex->get('USD') . "\n";
} catch (Exception $e) {
echo "No data available for $date\n";
}
}
$forex = new Forex();
$forex->load('https://example.com/custom-ecb-mirror.xml');
$forex = new Forex();
$forex->load('file:///path/to/local/eurofxref-daily.xml');
$forex = new Forex();
$forex->load(Forex::ECB_DAILY);
// Switch source
$forex->load(Forex::ECB_90DAYS);