-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdarganapi.php
56 lines (38 loc) · 1.3 KB
/
darganapi.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
<?php
error_reporting(E_ALL);
$find_postcode = $_POST['postcode'];
$find_distance = $_POST['distance'];
//Miles to KM
$radius = str_replace("Within ", "", $find_distance );
$radius = str_replace(" miles", "", $radius);
$radius = round(floatval($radius) * 1.60934, 1);
//Format Postcode
$postcode = str_replace(" ", "", strtoupper($find_postcode));
//API
$curl = curl_init();
$endpoint = "https://dargan.p.rapidapi.com/inradius?postcode=$postcode&radius=$radius";
curl_setopt_array($curl, [
CURLOPT_URL => $endpoint,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => [
"X-RapidAPI-Host: dargan.p.rapidapi.com",
"X-RapidAPI-Key: <Use your API KEY Here>"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
$data = json_decode($response, true);
foreach($data as $result){
echo $result['Postcode'] . "\n";
}
}