false, 'error' => 'Method not allowed']); exit; } $prompt = $_POST['prompt'] ?? ''; $musicLengthMs = (int)($_POST['music_length_ms'] ?? 0); $apiKey = $config['elevenlabs_api_key']; if (empty($prompt)) { echo json_encode(['success' => false, 'error' => 'Prompt is required']); exit; } if (empty($apiKey) || $apiKey === 'your-api-key-here') { echo json_encode(['success' => false, 'error' => 'ElevenLabs API key not configured']); exit; } // Generate composition plan $planResult = generateCompositionPlan($prompt, $musicLengthMs, $apiKey); if ($planResult['success']) { echo json_encode(['success' => true, 'plan' => $planResult['plan']]); } else { echo json_encode(['success' => false, 'error' => $planResult['error']]); } function generateCompositionPlan($prompt, $musicLengthMs, $apiKey) { $url = 'https://api.elevenlabs.io/v1/music/plan'; $data = [ 'prompt' => $prompt, 'model_id' => 'music_v1' ]; // Add music length if specified (minimum 10 seconds) if ($musicLengthMs >= 10000) { $data['music_length_ms'] = $musicLengthMs; } $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'xi-api-key: ' . $apiKey, 'Content-Type: application/json' ]); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_TIMEOUT, 60); $response = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); if (curl_error($ch)) { curl_close($ch); return ['success' => false, 'error' => 'Connection error: ' . curl_error($ch)]; } curl_close($ch); if ($httpCode === 200) { $planData = json_decode($response, true); if ($planData) { return ['success' => true, 'plan' => $planData]; } else { return ['success' => false, 'error' => 'Invalid response format']; } } else { $errorResponse = json_decode($response, true); $errorMessage = $errorResponse['detail'] ?? 'Unknown error occurred'; return ['success' => false, 'error' => 'API Error (' . $httpCode . '): ' . $errorMessage]; } } ?>