php separate
This commit is contained in:
57
php/camlist.php
Normal file
57
php/camlist.php
Normal file
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
require_once __DIR__ . '/common.php';
|
||||
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
|
||||
// Initialize response array
|
||||
$response = [];
|
||||
|
||||
// Database connection
|
||||
try {
|
||||
$dbconn = getDBConnection();
|
||||
|
||||
// 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);
|
||||
|
||||
} 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);
|
||||
|
||||
// Close database connection when needed
|
||||
if (isset($dbconn)) {
|
||||
pg_close($dbconn);
|
||||
}
|
||||
?>
|
||||
Reference in New Issue
Block a user