What are some free or cost-effective options for fetching and displaying currency exchange rates in PHP applications?
Fetching and displaying currency exchange rates in PHP applications can be achieved by using free or cost-effective API services such as exchangeratesapi.io or currencyconverterapi.com. These APIs provide real-time exchange rates for various currencies and can be easily integrated into PHP applications using HTTP requests.
<?php
// Fetching currency exchange rates using exchangeratesapi.io API
$base_currency = 'USD';
$target_currency = 'EUR';
$api_key = 'YOUR_API_KEY';
$url = "https://api.exchangeratesapi.io/latest?base=$base_currency&symbols=$target_currency&apikey=$api_key";
$response = file_get_contents($url);
$data = json_decode($response, true);
$exchange_rate = $data['rates'][$target_currency];
echo "1 $base_currency = $exchange_rate $target_currency";
?>
Related Questions
- What are best practices for handling dynamic text formats, such as varying numbers of spaces or punctuation, when extracting data in PHP?
- What is the difference between using || and <= in a while loop in PHP?
- What potential pitfalls are associated with defining functions dependent on the request method in PHP?