zibo-dashboard/public/userarea/schemi_liberi.php
2025-06-05 10:11:57 +02:00

124 lines
3.8 KiB
PHP

<?php
header('Content-Type: application/json');
$base_url = 'https://93.43.5.102/limsapi';
$credentials = [
'Username' => 'WebApiUser',
'Password' => 'webapiuser01'
];
// Autenticazione → ottieni token
$ch = curl_init("$base_url/api/authentication/authenticate");
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'
]);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_VERBOSE, true);
$log = fopen('curl_auth_debug.log', 'w');
curl_setopt($ch, CURLOPT_STDERR, $log);
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$curl_error = curl_error($ch);
fclose($log);
curl_close($ch);
// Verifica la risposta
if ($response === false || $http_code != 200) {
http_response_code($http_code ? $http_code : 500);
echo json_encode([
'error' => 'Errore nella richiesta di autenticazione',
'http_code' => $http_code,
'curl_error' => $curl_error,
'raw_response' => substr($response, 0, 1000)
]);
exit;
}
// Decodifica la risposta JSON
$token_data = json_decode($response, true);
// Gestisci sia il caso in cui la risposta è una stringa (token diretto) sia un oggetto con chiave 'token'
$token = null;
if (is_array($token_data) && isset($token_data['token'])) {
$token = $token_data['token'];
} elseif (is_string($token_data) && !empty($token_data)) {
$token = trim($token_data, '"');
} elseif (is_string($response) && !empty($response)) {
$token = trim($response, '"');
}
if (empty($token)) {
http_response_code(401);
echo json_encode([
'error' => 'Token non ricevuto',
'http_code' => $http_code,
'curl_error' => $curl_error,
'raw_response' => substr($response, 0, 1000)
]);
exit;
}
// Chiamata GET per recuperare gli schemi con IdCliente = 0
$cliente_id = 0; // Definiamo l'ID cliente
$endpoint = "$base_url/api/odata/Cliente($cliente_id)?\$expand=SchemiAbilitati"; // Nota: usiamo \$ per escape in PHP
$ch = curl_init($endpoint);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"Authorization: Bearer $token",
"Accept: application/json"
]);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_VERBOSE, true);
$log = fopen('curl_schemi_liberi_debug.log', 'w');
curl_setopt($ch, CURLOPT_STDERR, $log);
$schemi_response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$curl_error = curl_error($ch);
fclose($log);
curl_close($ch);
// Gestisci la risposta
if ($schemi_response === false) {
http_response_code(500);
echo json_encode([
'error' => 'Errore nella richiesta cURL',
'curl_error' => $curl_error
]);
exit;
}
if ($http_code === 200) {
$schemi_data = json_decode($schemi_response, true);
if (json_last_error() === JSON_ERROR_NONE) {
echo json_encode($schemi_data);
} else {
http_response_code(500);
echo json_encode([
'error' => 'Risposta non JSON valida',
'http_code' => $http_code,
'response' => substr($schemi_response, 0, 1000)
]);
}
} else {
$schemi_data = json_decode($schemi_response, true);
if (json_last_error() === JSON_ERROR_NONE) {
echo json_encode($schemi_data);
} else {
http_response_code($http_code);
echo json_encode([
'error' => 'Errore nel recupero schemi',
'http_code' => $http_code,
'curl_error' => $curl_error,
'response' => substr($schemi_response, 0, 1000)
]);
}
}