Files
crm.clientright.ru/analyze_case_proxy.php

60 lines
2.1 KiB
PHP
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
// analyze_case_proxy.php - 100% совместим с PHP 7.0
// 1. Получаем параметры (максимально простой способ)
$caseId = isset($_POST['id']) ? $_POST['id'] : (isset($_GET['id']) ? $_GET['id'] : '');
$description = isset($_POST['query_text']) ? $_POST['query_text'] : (isset($_GET['query_text']) ? $_GET['query_text'] : '');
// 2. Валидация
if (empty($caseId)) {
header('Content-Type: application/json');
die('{"status":"error","message":"Missing ID"}');
}
// 3. Подготовка запроса
$request = array(
'id' => $caseId,
'description' => $description
);
// 4. Отправка в n8n (упрощённый cURL)
$ch = curl_init('https://n8n.clientright.pro/webhook-test/analyze-case');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($request));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$response = curl_exec($ch);
curl_close($ch);
// 5. Очистка ответа (удаляем BOM если есть)
if (substr($response, 0, 3) == pack('CCC', 0xEF, 0xBB, 0xBF)) {
$response = substr($response, 3);
}
// 6. Парсинг ответа (с защитой от ошибок)
$result = array(
'jobId' => '',
'case_id' => $caseId, // сохраняем исходный ID как fallback
'Id' => 0,
'moderationVerdict' => 'passed'
);
if (!empty($response)) {
$data = json_decode($response, true);
if (is_array($data)) {
// Обрабатываем как массив или одиночный объект
$item = isset($data[0]) ? $data[0] : $data;
if (isset($item['jobId'])) $result['jobId'] = $item['jobId'];
if (isset($item['case_id'])) $result['case_id'] = $item['case_id'];
if (isset($item['Id'])) $result['Id'] = intval($item['Id']);
if (isset($item['moderationVerdict'])) {
$result['moderationVerdict'] = $item['moderationVerdict'];
}
}
}
// 7. Отправляем результат
header('Content-Type: application/json');
echo json_encode($result);
?>