addition API BV

This commit is contained in:
2025-06-05 10:11:57 +02:00
parent d925726ecd
commit e8b15d8096
22 changed files with 1335 additions and 470 deletions
+14 -39
View File
@@ -17,63 +17,38 @@ curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($credentials));
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Accept: application/json',
'User-Agent: Mozilla/5.0 (compatible; PHP cURL)'
'Accept: application/json'
]);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Disabilita verifica SSL (solo test)
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // Disabilita verifica host (solo test)
curl_setopt($ch, CURLOPT_VERBOSE, true); // Abilita debug
$log = fopen('curl_debug.log', 'a'); // Usa 'a' per appendere al log
// Gestione degli errori di apertura del file di log
if ($log === false) {
http_response_code(500);
echo json_encode([
'error' => 'Impossibile aprire il file di log per il debug'
]);
exit;
}
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Solo per test
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // Solo per test
curl_setopt($ch, CURLOPT_VERBOSE, true);
$log = fopen('curl_auth_debug.log', 'w');
curl_setopt($ch, CURLOPT_STDERR, $log);
// Esegui la richiesta
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$curl_error = curl_error($ch);
fclose($log);
// Log della risposta per debug
file_put_contents('curl_response.log', "HTTP Code: $http_code\nResponse: $response\n\n", FILE_APPEND);
// Verifica errori di esecuzione cURL
if ($response === false) {
// Verifica errori
if ($response === false || $http_code != 200) {
http_response_code($http_code ? $http_code : 500);
echo json_encode([
'error' => 'Errore nella richiesta API',
'http_code' => $http_code,
'curl_error' => $curl_error
'curl_error' => $curl_error,
'response' => substr($response, 0, 1000)
]);
curl_close($ch);
exit;
}
// Rimuovi eventuali virgolette e spazi dalla risposta
$trimmed_response = trim($response, '" \t\n\r\0\x0B');
// Se la risposta è una stringa non vuota, considerala il token
if (is_string($trimmed_response) && !empty($trimmed_response)) {
http_response_code($http_code);
echo json_encode(['token' => $trimmed_response]);
} else {
// Tenta di decodificare la risposta come JSON
$decoded = json_decode($response, true);
if (json_last_error() === JSON_ERROR_NONE && isset($decoded['token']) && is_string($decoded['token']) && !empty($decoded['token'])) {
$decoded = json_decode($response);
if (json_last_error() === JSON_ERROR_NONE) {
http_response_code($http_code);
echo json_encode(['token' => $decoded['token']]);
echo $response;
} else {
http_response_code($http_code);
http_response_code(500);
echo json_encode([
'error' => 'Risposta non valida o token non trovato',
'error' => 'Risposta non JSON valida',
'http_code' => $http_code,
'response' => substr($response, 0, 1000)
]);