How can PHP be used to automatically fetch and convert currency exchange rates?
To automatically fetch and convert currency exchange rates using PHP, you can use an API like Open Exchange Rates or CurrencyLayer to retrieve the latest exchange rates. You can then parse the JSON response and calculate the conversion based on the desired currencies.
<?php
// API endpoint to fetch the latest exchange rates
$api_url = 'https://api.exchangerate-api.com/v4/latest/USD';
// Fetching data from API
$response = file_get_contents($api_url);
$data = json_decode($response, true);
// Conversion rate from USD to EUR
$usd_to_eur = $data['rates']['EUR'];
// Amount to convert
$usd_amount = 100;
$eur_amount = $usd_amount * $usd_to_eur;
echo $usd_amount . ' USD is equal to ' . $eur_amount . ' EUR';
?>