'Failed to fetch videos for query: ' . $query, 'videos' => []]); exit; } $videos = []; foreach ($data['videos'] as $video) { $video_file = null; // Find a suitable video file link foreach ($video['video_files'] as $file) { if ($file['quality'] === 'hd' && (strpos($file['link'], '.mp4') !== false)) { $video_file = $file; break; } } // Fallback to any mp4 file if (!$video_file) { foreach ($video['video_files'] as $file) { if (strpos($file['link'], '.mp4') !== false) { $video_file = $file; break; } } } if ($video_file) { $videos[] = [ 'id' => $video['id'], 'thumbnail' => $video['image'], 'download_url' => $video_file['link'], 'photographer' => $video['user']['name'] ?? 'Unknown', 'photographer_url' => $video['user']['url'] ?? '', ]; } } // Second-level filtering for haram terms in video tags, if available $filtered_videos = array_filter($videos, function($video) use ($haram_elements) { if (isset($video['tags']) && is_array($video['tags'])) { foreach ($haram_elements as $haram) { if (in_array($haram, $video['tags'])) { return false; } } } return true; }); echo json_encode(['videos' => array_values($filtered_videos)]);