Hi guys,
I would really love to get some advice here. I am using a live currency exchange API i’v created a foreach loop
to display every single CURRENCY inside an array. I would like to know if i am doing this smart… or if there is one better way on how to do it… i would really appreciate it if somebody could review my code for real.
<!doctype HTML>
<html>
<head>
<title>Currency Exchange</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<style>
.container {
max-width: 992px;
}
.h1 {
text-align: center;
}
.ce-wrapper {
margin: 20px auto;
background-color: #F7F7F7;
border: 1px solid #F4F3F3;
padding: 20px;
}
</style>
</head>
<body>
<div class="container ce-wrapper">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<h1 class="h1">Wisselkoers</h1>
<!-- Currency Exchange -->
<?php
// set API Endpoint and access key (and any options of your choice)
$endpoint = 'live'; // Live modus
$access_key = 'REMOVED_THIS_FOR_SECURITY_REASONS'; // Access Key ID
$valuta = 'EUR,GBP,CAD,PLN,AWG,YTL,HRK'; // Beschikbare valuta's
// Initialize CURL:
$ch = curl_init('http://apilayer.net/api/'.$endpoint.'?access_key='.$access_key.'¤cies='.$valuta.'');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Store the data:
$json = curl_exec($ch);
curl_close($ch);
// Decode JSON response:
$exchangeRates = json_decode($json, true);
?>
<!--
<pre>
<?//php print_r($exchangeRates); ?>
</pre>
-->
<?php foreach($exchangeRates['quotes'] as $key => $rate) { ?>
<?php echo "$key:" . $rate . "<br>"; ?>
<?php } ?>
<br>
<?php $initJSON = json_encode($exchangeRates['quotes'], JSON_PRETTY_PRINT);
echo $initJSON;
?>
<script id="initJSON" type="application/json">
<?php echo $initJSON; ?>
</script>
</div>
</div>
</div>
<!-- JS -->
<script src="https://code.jquery.com/jquery-1.12.3.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</body>
</html>
now… i am wondering if there is a better way on how to DISPLAY a JSON output inside a SCRIPT TAG… without using PHP…
i do get this as result and no errors at all:
<script id="initJSON" type="application/json">
{
"USDEUR": 0.879121,
"USDGBP": 0.691922,
"USDCAD": 1.2842,
"USDPLN": 3.87965,
"USDAWG": 1.79,
"USDHRK": 6.59435
} </script>
Thanks in advance!!!