84 lines
2.7 KiB
PHP
84 lines
2.7 KiB
PHP
<?php
|
|
header('Content-Type: application/json');
|
|
|
|
// URL dell'API e credenziali
|
|
$api_url = 'https://93.43.5.102/limsapi/api/authentication/authenticate';
|
|
$credentials = [
|
|
'Username' => 'WebApiUser',
|
|
'Password' => 'webapiuser01'
|
|
];
|
|
|
|
// Inizializza cURL
|
|
$ch = curl_init($api_url);
|
|
|
|
// Configura le opzioni di cURL
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
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)'
|
|
]);
|
|
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_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) {
|
|
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_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'])) {
|
|
http_response_code($http_code);
|
|
echo json_encode(['token' => $decoded['token']]);
|
|
} else {
|
|
http_response_code($http_code);
|
|
echo json_encode([
|
|
'error' => 'Risposta non valida o token non trovato',
|
|
'http_code' => $http_code,
|
|
'response' => substr($response, 0, 1000)
|
|
]);
|
|
}
|
|
}
|
|
|
|
curl_close($ch);
|