You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
103 lines
2.5 KiB
103 lines
2.5 KiB
<?php
|
|
/**
|
|
* RX - Main API to process in coming request for closest location.
|
|
* 2019-09-25
|
|
* Rick Hays
|
|
*
|
|
*/
|
|
|
|
include_once 'classes/geocode.php';
|
|
|
|
// Test Key before allowing access - No encryption, simple key test.
|
|
$key = GetParam('K');
|
|
if ($key !== 'RLH1234')
|
|
{
|
|
header("HTTP/1.0 405 Method Not Allowed");
|
|
exit;
|
|
}
|
|
|
|
$request_method = strtoupper($_SERVER["REQUEST_METHOD"]);
|
|
switch($request_method)
|
|
{
|
|
case 'GET':
|
|
|
|
$Lat = GetParam('Latitude');
|
|
$Lon = GetParam('Longitude');
|
|
try
|
|
{
|
|
$db = new PDO('sqlite:db/RX.db');
|
|
$geo = new geocode();
|
|
$shortest = 999999; // <-- Sets to a very high value to find shortest distance below.
|
|
|
|
$result = $db->query('SELECT rowid,* FROM pharmacies');
|
|
foreach ($result as $row) {
|
|
$distance = $geo->straight_line_distance($Lat, $Lon, $row['Latitude'], $row['Longitude'], 'N');
|
|
if ($distance < $shortest)
|
|
{
|
|
$pharmacy = $row['Pharmacy'];
|
|
$address = $row['Address'];
|
|
$city = $row['City'];
|
|
$state = $row['State'];
|
|
$zip = $row['Zip'];
|
|
$latitude = $row['Latitude'];
|
|
$longitude = $row['Longitude'];
|
|
$shortest = $distance;
|
|
}
|
|
}
|
|
$db = NULL;
|
|
}
|
|
catch(PDOException $e)
|
|
{
|
|
echo 'PDO Exception: ' . $e-> getMessage();
|
|
exit;
|
|
}
|
|
break;
|
|
// case 'POST':
|
|
// break;
|
|
// case 'PUT':
|
|
// break;
|
|
// case 'DELETE':
|
|
// break;
|
|
default:
|
|
// Invalid Request Method
|
|
header("HTTP/1.0 405 Method Not Allowed");
|
|
exit;
|
|
}
|
|
|
|
// Output JSON back to HTML file.
|
|
echo 'Closest Pharmacy To you is: <br/><br/>';
|
|
echo $pharmacy . '<br/>';
|
|
echo $address . '<br/>';
|
|
echo $city . '<br/>';
|
|
echo $state . '<br/>';
|
|
echo $zip . '<br/>';
|
|
echo '<br/>';
|
|
// echo $latitude . '<br/>';
|
|
// echo $longitude . '<br/>';
|
|
echo 'Distance of - ' . round($shortest, 1) . ' mile(s) away.<br/>';
|
|
exit;
|
|
|
|
// ##### END OF PROGRAM ################################################################################################
|
|
|
|
/**
|
|
* GetParam - Returns the GET or POST values, and allows for defaults if not present.
|
|
* @param $param_name
|
|
* @param null $default
|
|
*
|
|
* @return string|null
|
|
*/
|
|
function GetParam($param_name, $default=NULL)
|
|
{
|
|
global $_POST;
|
|
global $_GET;
|
|
$param_value = "";
|
|
|
|
if(isset($_POST[$param_name]))
|
|
$param_value = $_POST[$param_name];
|
|
else if(isset($_GET[$param_name]))
|
|
$param_value = $_GET[$param_name];
|
|
else if($param_value === '')
|
|
$param_value = $default;
|
|
|
|
return $param_value;
|
|
}
|
|
|