mirror of
https://git.sr.ht/~iwakuralain/text0Nly
synced 2025-07-27 15:36:11 +00:00
added messages reader by api
This commit is contained in:
parent
44ad71a116
commit
ea69fc799a
16
main/api.php
16
main/api.php
@ -13,6 +13,22 @@ $db = new PDO(
|
||||
);
|
||||
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
|
||||
$limit = isset($_GET['limit']) ? (int)$_GET['limit'] : 50;
|
||||
$limit = min(max(1, $limit), 100);
|
||||
|
||||
try {
|
||||
$stmt = $db->prepare('SELECT username, message, created_at, signature, is_encrypted FROM messages ORDER BY created_at DESC LIMIT ?');
|
||||
$stmt->execute([$limit]);
|
||||
$messages = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
echo json_encode(['messages' => $messages]);
|
||||
} catch (Exception $e) {
|
||||
http_response_code(500);
|
||||
echo json_encode(['error' => 'Server error']);
|
||||
}
|
||||
exit;
|
||||
}
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
||||
http_response_code(405);
|
||||
die(json_encode(['error' => 'Method not allowed']));
|
||||
|
Loading…
x
Reference in New Issue
Block a user