'error', 'message' => 'Only POST requests are allowed.']); exit; } $postData = json_decode(file_get_contents('php://input'), true); $postid = htmlspecialchars(trim($postData['spostid'] ?? '')); if (!$postid) { echo json_encode(['status' => 'error', 'message' => 'Invalid post ID.']); exit; } $cacheKey = "posts:$postid"; $firstThree = substr($postid, 0, 3); $tabletoselectfrom = $firstThree === 'vcx' ? 'comments' : 'posts'; $redis = new Redis(); try { $redis->connect('127.0.0.1', 6379); } catch (Exception $e) { echo json_encode(['status' => 'error', 'message' => 'Redis connection failed.']); exit; } // Try Redis $cachedData = $redis->get($cacheKey); if ($cachedData !== false) { $row = json_decode($cachedData, true); $media = json_decode($row['mediaurl'] ?? '[]', true) ?? []; $originals = json_decode($row['originalfilename'] ?? '[]', true) ?? []; $posts[] = [ 'id' => $row['id'], 'accountId' => $row['accountId'], // already casted as (int) when cached 'username' => $row['username'], // not 'defaultusername' 'posttype' => '--', 'content' => $row['content'] . ' cahce...s', 'media' => $row['media'] ?? null, 'audiofilename' => $row['audiofilename'], 'originals' => $row['originals'] ?? [], 'repostid' => $row['repostid'] ?? '', 'hashtags' => $row['hashtags'], 'niche' => $row['niche'], 'ip' => $row['ip'], 'userAgent' => $row['userAgent'], 'postId' => $row['postId'], 'likes' => (int)$row['likes'], 'comments' => (int)$row['comments'], 'shares' => (int)$row['shares'], 'viewType' => $row['viewType'], 'svd' => $row['svd'] ?? 'no', 'reposted' => $row['reposted'] ?? 'no', 'bookmarks' => $row['bookmarks'] ?? 0, 'numberofrepost' => $row['numberofrepost'] ?? 0, 'mutestatus' => $row['mutestatus'], 'following' => $row['following'], 'notintrested' => $row['notintrested'], 'shlink' => $row['shlink'], 'blockedstatus' => $row['blockedstatus'], 'potentialImpressions' => (int)$row['potentialImpressions'], 'impressions' => (int)$row['impressions'], 'liked' => $row['liked'], 'nsfw' => $row['nsfw'] ]; echo json_encode(['status' => 'success', 'posts' => $posts]); exit; } // Fallback to DB include '../severdbconnect.php'; $sql = "SELECT * FROM $tabletoselectfrom WHERE postid = ? LIMIT 1"; $stmt = $conn->prepare($sql); if (!$stmt) { echo json_encode(['status' => 'error', 'message' => 'Prepare failed: ' . $conn->error]); exit; } $stmt->bind_param('s', $postid); $stmt->execute(); $result = $stmt->get_result(); if ($row = $result->fetch_assoc()) { $media = json_decode($row['mediaurl'] ?? '[]', true) ?? []; $originals = json_decode($row['originalfilename'] ?? '[]', true) ?? []; $posts[] = [ 'id' => $row['id'], 'accountId' => (int)$row['defaultaccountid'], 'username' => $row['defaultusername'], 'posttype' => '--' ?? null, 'content' => $row['postcontent'], 'media' => $media ?? null, 'audiofilename' => $row['audiofilename'] ?? '', 'originals' => $originals, 'repostid' => $row['repostid'] ?? null, 'hashtags' => 'dance', 'niche' => $row['niche'] ?? '', 'ip' => $row['ipaddress'] ?? null, 'userAgent' => $row['useragent'] ?? null, 'postId' => $row['postid'], 'likes' => (int)($row['totallikes'] ?? 0), 'comments' => (int)($row['totalcomment'] ?? 0), 'shares' => (int)($row['totalshares'] ?? 0), 'viewType' => $row['distributedviewstype'] ?? null, 'svd' => 'no', 'reposted' => 'no', 'bookmarks' => 0, 'numberofrepost' => 0, 'mutestatus' => 'no', 'following' => 'no', 'notintrested' => 'no', 'shlink' => 'https://voidvenus.online/sharelink', 'blockedstatus' => 'no', 'potentialImpressions' => (int)($row['assignedpotentialimpressions'] ?? 0), 'impressions' => (int)($row['totalimpressions'] ?? 0), 'liked' => 'no', 'nsfw' => $row['nsfw'] ?? 'none' ]; // Save full payload to Redis $cachedFormattedPost = [ 'id' => $row['id'], 'accountId' => (int)$row['defaultaccountid'], 'username' => $row['defaultusername'], 'posttype' => '--', 'content' => $row['postcontent'], 'media' => $media ?? null, 'audiofilename' => $row['audiofilename'], 'originals' => $originals, 'repostid' => $row['repostid'], 'hashtags' => array_filter(array_map('trim', explode(',', $row['hashtag']))), 'niche' => $row['niche'], 'ip' => $row['ipaddress'], 'userAgent' => $row['useragent'], 'postId' => $row['postid'], 'likes' => (int)$row['totallikes'], 'comments' => (int)$row['totalcomment'], 'shares' => (int)$row['totalshares'], 'viewType' => $row['distributedviewstype'], 'svd' => 'no', 'reposted' => 'no', 'bookmarks' => 0, 'numberofrepost' => 0, 'mutestatus' => 'no', 'following' => 'no', 'notintrested' => 'no', 'shlink' => 'https://voidvenus.online/sharelink', 'blockedstatus' => 'no', 'potentialImpressions' => (int)$row['assignedpotentialimpressions'], 'impressions' => (int)$row['totalimpressions'], 'liked' => 'no', 'nsfw' => $row['nsfw'] ]; $redis->setex($cacheKey, 60, json_encode($cachedFormattedPost)); ; echo json_encode(['status' => 'success', 'posts' => $posts]); } else { echo json_encode(['status' => 'error', 'message' => 'Post not found in database']); } $stmt->close(); $conn->close();