-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.php
68 lines (54 loc) · 1.76 KB
/
api.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
$endpoint = 'latest';
$base = 'EUR';
$token = 'YOUR-ACCESS-TOKEN';
$url = "http://data.fixer.io/api/{$endpoint}?base={$base}&access_key={$token}"; // API URL
function getInfo($hostUrl)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $hostUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // change to 1 to verify cert
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
$currencyRateList = curl_exec($ch);
return $currencyRateList;
}
$currency = json_decode(getInfo($url) , true);
// API Response Status
$status = ($currency['success']);
if ($status == true) {
$success = true;
$base = 'USD';
$exchangeProvider = 'Fixer';
$timestamp = $currency['timestamp'];
$date = $currency['date'];
// Currency values
$inr = $currency['rates']['INR'] / $currency['rates'][$base]; // India
$sgd = $currency['rates']['SGD'] / $currency['rates'][$base]; // Singapore
$aed = $currency['rates']['AED'] / $currency['rates'][$base]; // United Arab Emirates
$gbp = $currency['rates']['GBP'] / $currency['rates'][$base]; // United Kingdom
$resp = json_encode(array(
'success' => $success,
'base' => $base,
'exchangeProvider' => $exchangeProvider,
'date' => $date,
'timestamp' => $timestamp,
'rates' => array(
'INR' => $inr,
'SGD' => $sgd,
'AED' => $aed,
'GBP' => $gbp
)
));
echo ($resp);
} else {
$success = false;
$info = "Unable to handle exchange request";
$resp = json_encode(array(
'success' => $success,
'info' => $info
));
echo ($resp);
}