Created
July 15, 2019 20:40
-
-
Save wordpress-lab/63b2dee6742c8207917cbce784834653 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Fetching JSON | |
$req_url = 'https://api.exchangerate-api.com/v4/latest/USD'; | |
//$req_url = 'https://api.exchangerate-api.com/v4/latest/EUR'; | |
$response_json = file_get_contents($req_url); | |
// Continuing if we got a result | |
if(false !== $response_json) { | |
// Try/catch for json_decode operation | |
try { | |
// Decoding | |
$response_object = json_decode($response_json); | |
// YOUR APPLICATION CODE HERE, e.g. | |
$base_price = 1; // Your price in USD | |
if(isset($_POST['submit'])) | |
{ | |
$base_price = isset($_POST['price'])?$_POST['price']:1; | |
$rate = isset($_POST['rates'])?$_POST['rates']:1; | |
$EUR_price = round(($base_price * $rate), 2); | |
echo "Rate: ". $EUR_price; | |
} | |
} | |
catch(Exception $e) { | |
// Handle JSON parse error... | |
} | |
} | |
?> | |
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> | |
<input type="text" name="price"><br> | |
<select name="rates"> | |
<?php foreach ($response_object->rates as $key => $value) { ?> | |
<option value="<?php echo $value; ?>"><?php echo $key; ?></option> | |
<?php } ?> | |
</select> | |
<input type="submit" name="submit" value="Submit "><br> | |
</form> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment