This commit is contained in:
2026-02-19 11:45:25 +01:00
parent a8caee15f9
commit c718f15f09
9 changed files with 129 additions and 239 deletions

View File

@@ -187,39 +187,102 @@ function checkRateLimit($ip) {
function sendToWebhook($data, $webhookUrl = null) {
$url = $webhookUrl ?? WEBHOOK_URL;
$jsonData = json_encode($data);
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => json_encode($data),
CURLOPT_HTTPHEADER => [
'Content-Type: application/json',
'User-Agent: Profice-Web-API/2.0'
],
CURLOPT_TIMEOUT => 30,
CURLOPT_SSL_VERIFYPEER => false, // Disable SSL verification for n8n connection
CURLOPT_SSL_VERIFYHOST => false, // Disable host verification
CURLOPT_FOLLOWLOCATION => true
]);
// Log the attempt
error_log("Webhook attempt - URL: " . $url . " - Data length: " . strlen($jsonData));
// Try cURL first if available
if (function_exists('curl_init')) {
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $jsonData,
CURLOPT_HTTPHEADER => [
'Content-Type: application/json',
'Accept: application/json',
'User-Agent: Profice-Web-API/2.0'
],
CURLOPT_TIMEOUT => 30,
CURLOPT_CONNECTTIMEOUT => 10,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_MAXREDIRS => 3
]);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$error = curl_error($ch);
$errno = curl_errno($ch);
curl_close($ch);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$error = curl_error($ch);
$errno = curl_errno($ch);
curl_close($ch);
if ($error) {
error_log("Webhook Error [$errno]: " . $error . " - URL: " . $url);
return ['success' => false, 'error' => $error, 'errno' => $errno, 'response' => $response];
error_log("Webhook cURL response - HTTP: $httpCode, Error: $error, Response: " . substr($response, 0, 500));
if (!$error && $httpCode > 0) {
return [
'success' => $httpCode >= 200 && $httpCode < 300,
'http_code' => $httpCode,
'response' => $response,
'error' => $error,
'method' => 'curl'
];
}
error_log("Webhook cURL failed [$errno]: $error - trying file_get_contents fallback");
}
// Fallback to file_get_contents if cURL fails or unavailable
$context = stream_context_create([
'http' => [
'method' => 'POST',
'header' => "Content-Type: application/json\r\nAccept: application/json\r\nUser-Agent: Profice-Web-API/2.0\r\n",
'content' => $jsonData,
'timeout' => 30,
'ignore_errors' => true
],
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
]
]);
$response = @file_get_contents($url, false, $context);
// Get HTTP response code from headers
$httpCode = 0;
if (isset($http_response_header) && is_array($http_response_header)) {
foreach ($http_response_header as $header) {
if (preg_match('/^HTTP\/\d+\.?\d*\s+(\d+)/', $header, $matches)) {
$httpCode = (int)$matches[1];
}
}
}
error_log("Webhook file_get_contents response - HTTP: $httpCode, Response: " . substr($response ?: '', 0, 500));
if ($response !== false) {
return [
'success' => $httpCode >= 200 && $httpCode < 300,
'http_code' => $httpCode,
'response' => $response,
'error' => null,
'method' => 'file_get_contents'
];
}
$lastError = error_get_last();
error_log("Webhook all methods failed - Last error: " . json_encode($lastError));
return [
'success' => $httpCode >= 200 && $httpCode < 300,
'http_code' => $httpCode,
'response' => $response,
'error' => $error
'success' => false,
'error' => $lastError['message'] ?? 'All HTTP methods failed',
'errno' => 0,
'response' => null,
'method' => 'none'
];
}