$statusCode ]; if (isset($data['error'])) { $response['error'] = $data['error']; } else if (isset($data['url'])) { $response['url'] = $data['url']; } echo json_encode($response); exit; } // Basic URL validation // Validação básica da URL if (!$url || !filter_var($url, FILTER_VALIDATE_URL)) { sendResponse([ 'error' => [ 'type' => URLAnalyzer::ERROR_INVALID_URL, 'message' => Language::getMessage('INVALID_URL')['message'] ] ], 400); } try { // Instantiate URL analyzer // Instancia o analisador de URLs $analyzer = new URLAnalyzer(); // Try to analyze the provided URL // Tenta analisar a URL fornecida $analyzer->analyze($url); // If analysis is successful, return the processed URL // Se a análise for bem-sucedida, retorna a URL processada sendResponse([ 'url' => SITE_URL . '/p/' . $url ], 200); } catch (URLAnalyzerException $e) { // Get error details from the exception // Obtém detalhes do erro da exceção $errorType = $e->getErrorType(); $additionalInfo = $e->getAdditionalInfo(); // Add error header for better client-side handling // Adiciona header de erro para melhor tratamento no cliente header('X-Error-Type: ' . $errorType); if ($additionalInfo) { header('X-Error-Info: ' . $additionalInfo); } sendResponse([ 'error' => [ 'type' => $errorType, 'message' => $e->getMessage(), 'details' => $additionalInfo ?: null ] ], $e->getCode()); } catch (Exception $e) { // Handle any other unexpected errors // Trata quaisquer outros erros inesperados sendResponse([ 'error' => [ 'type' => URLAnalyzer::ERROR_GENERIC_ERROR, 'message' => Language::getMessage('GENERIC_ERROR')['message'] ] ], 500); } } else { // Return 404 error for endpoints not found // Retorna erro 404 para endpoints não encontrados sendResponse([ 'error' => [ 'type' => URLAnalyzer::ERROR_NOT_FOUND, 'message' => Language::getMessage('NOT_FOUND')['message'] ] ], 404); }