-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch_data.php
More file actions
32 lines (25 loc) · 998 Bytes
/
fetch_data.php
File metadata and controls
32 lines (25 loc) · 998 Bytes
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
<?php
// fetch_data.php
header('Content-Type: application/json');
// MongoDB connection details
$mongoUri = 'mongodb://localhost:27017/'; // Adjust as needed
$databaseName = 'gunshot_db'; // Replace with your database name
$collectionName = 'gunshot_direction'; // Replace with your collection name
try {
// Create a MongoDB client instance
$client = new MongoDB\Client($mongoUri);
$collection = $client->selectCollection($databaseName, $collectionName);
// Fetch the latest document from the collection
$latestData = $collection->findOne([], ['sort' => ['timestamp' => -1]]);
// Prepare the data for JSON output
$response = [
'timestamp' => $latestData->timestamp,
'direction' => $latestData->direction
];
// Output the JSON data
echo json_encode($response);
} catch (Exception $e) {
// Handle any errors and output an error message
echo json_encode(['error' => 'Unable to fetch data: ' . $e->getMessage()]);
}
?>