56 lines
1.5 KiB
PHP
56 lines
1.5 KiB
PHP
<?php
|
|
header('Content-Type: application/json; charset=utf-8');
|
|
|
|
// Initialize response array
|
|
$response = [];
|
|
|
|
// Database connection
|
|
try {
|
|
$dbconn = pg_connect("host=localhost dbname=nws user=nws password=nws");
|
|
if (!$dbconn) {
|
|
throw new Exception('Could not connect to database');
|
|
}
|
|
|
|
// Performing SQL query
|
|
$query = "SELECT url, lat, lon, elevation, county, state, active, aspect, bloomsky, source, method FROM cams where active = true";
|
|
$result = pg_query($dbconn, $query);
|
|
|
|
if (!$result) {
|
|
throw new Exception('Query failed: ' . pg_last_error());
|
|
}
|
|
|
|
// Fetch results
|
|
$data = [];
|
|
while ($line = pg_fetch_array($result, null, PGSQL_ASSOC)) {
|
|
// Ensure numeric values are properly typed
|
|
$line['lat'] = floatval($line['lat']);
|
|
$line['lon'] = floatval($line['lon']);
|
|
$line['elevation'] = floatval($line['elevation']);
|
|
$line['active'] = $line['active'] === 't' ? true : false; // Convert PostgreSQL boolean
|
|
|
|
$data[] = $line;
|
|
}
|
|
|
|
$response = [
|
|
'status' => 'success',
|
|
'data' => $data,
|
|
'count' => count($data)
|
|
];
|
|
|
|
// Free resultset
|
|
pg_free_result($result);
|
|
|
|
// Close connection
|
|
pg_close($dbconn);
|
|
|
|
} catch (Exception $e) {
|
|
http_response_code(500);
|
|
$response = [
|
|
'status' => 'error',
|
|
'message' => $e->getMessage()
|
|
];
|
|
}
|
|
|
|
// Output JSON
|
|
echo json_encode($response, JSON_PRETTY_PRINT | JSON_NUMERIC_CHECK);
|
|
?>
|