'Parámetro "url" faltante']); exit; } $apiUrl = 'https://free.radioapi.lat/met?url=' . urlencode($url); // Conexión por cURL con mayor tolerancia $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $apiUrl); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_TIMEOUT, 10); curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0'); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); $response = curl_exec($ch); $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if ($httpcode !== 200 || !$response) { echo json_encode(['error' => 'No se pudo conectar a radioapi.lat']); exit; } $data = json_decode($response, true); if (!is_array($data)) { echo json_encode(['error' => 'Respuesta inválida']); exit; } // Reemplazar portada si viene por defecto if ( empty($data['artwork']) || str_contains($data['artwork'], 'nocover') || str_contains($data['artwork'], 'default') || str_contains($data['artwork'], 'cdn.radioapi.lat/cover/default') ) { $data['artwork'] = $defaultCover; } // Forzar que haya 9 canciones en el historial (rellenando si es necesario) $song_history = $data['song_history'] ?? []; while (count($song_history) < 9) { $song_history[] = [ 'song' => [ 'title' => $data['song'] ?? '', 'artist' => $data['artist'] ?? '' ] ]; } $data['song_history'] = array_slice($song_history, 0, 8); echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);