65 lines
1.9 KiB
PHP
65 lines
1.9 KiB
PHP
<?php
|
|
// search.php
|
|
|
|
// Setze den Content-Type auf JSON
|
|
header('Content-Type: application/json');
|
|
|
|
// Datenbank-Verbindungsparameter (bitte anpassen)
|
|
$host = 'localhost';
|
|
$db = 'web35_vysion';
|
|
$user = 'vysion_api';
|
|
$pass = '7e0pn9~2Z';
|
|
$charset = 'utf8mb4';
|
|
|
|
$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
|
|
$options = [
|
|
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
|
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
|
|
];
|
|
|
|
try {
|
|
$pdo = new PDO($dsn, $user, $pass, $options);
|
|
} catch (PDOException $e) {
|
|
http_response_code(500);
|
|
echo json_encode(['error' => 'Database connection failed: ' . $e->getMessage()]);
|
|
exit;
|
|
}
|
|
|
|
// Überprüfe, ob der Parameter "q" übergeben wurde
|
|
if (!isset($_GET['q'])) {
|
|
http_response_code(400);
|
|
echo json_encode(['error' => 'Missing query parameter "q".']);
|
|
exit;
|
|
}
|
|
|
|
// Den Query-Parameter auslesen und URL-dekodieren
|
|
$q = $_GET['q'];
|
|
$q_decoded = urldecode($q);
|
|
|
|
// Erwartetes Format: "from:username#hashtag"
|
|
// Beispiel: "from:silvesterhasani#bevysionary"
|
|
if (preg_match('/^from:([^#]+)#(.+)$/i', $q_decoded, $matches)) {
|
|
$username = $matches[1];
|
|
$hashtag = $matches[2];
|
|
} else {
|
|
http_response_code(400);
|
|
echo json_encode(['error' => 'Invalid query format. Expected format: "from:username#hashtag".']);
|
|
exit;
|
|
}
|
|
|
|
// SQL-Abfrage: Finde alle findings, die zum extrahierten username und hashtag passen
|
|
$sql = "SELECT h.hashtag, f.post_id, f.created_at, f.in_reply_to_id,
|
|
f.replies_count, f.reblogs_count, f.favorites_count,
|
|
f.acc_id, f.username, f.acct
|
|
FROM findings f
|
|
JOIN hashtags h ON f.hashtag_id = h.hashtag_id
|
|
WHERE f.username = :username AND h.hashtag = :hashtag";
|
|
|
|
$stmt = $pdo->prepare($sql);
|
|
$stmt->execute(['username' => $username, 'hashtag' => $hashtag]);
|
|
$results = $stmt->fetchAll();
|
|
|
|
// Gib das Ergebnis als JSON aus
|
|
echo json_encode($results);
|
|
?>
|