addition API BV
This commit is contained in:
@@ -0,0 +1,116 @@
|
||||
<?php
|
||||
header('Content-Type: application/json');
|
||||
|
||||
$base_url = 'https://93.43.5.102/limsapi';
|
||||
$credentials = [
|
||||
'Username' => 'WebApiUser',
|
||||
'Password' => 'webapiuser01'
|
||||
];
|
||||
|
||||
// Leggi l'ID del cliente dal body della richiesta
|
||||
$input = json_decode(file_get_contents('php://input'), true);
|
||||
$clienteId = isset($input['clienteId']) ? $input['clienteId'] : null;
|
||||
|
||||
if (!$clienteId) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['error' => 'ID cliente non fornito']);
|
||||
exit;
|
||||
}
|
||||
|
||||
// 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 del cliente
|
||||
$ch = curl_init("$base_url/api/odata/Cliente($clienteId)?\$expand=SchemiAbilitati");
|
||||
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_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);
|
||||
|
||||
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 {
|
||||
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)
|
||||
]);
|
||||
}
|
||||
Reference in New Issue
Block a user