Files
fantacalcio-4h/salva_voti.php
T

65 lines
1.9 KiB
PHP
Executable File

<?php
header('Content-Type: application/json');
// Percorsi dei file JSON
$votiFile = 'voti.json';
$giocatoriFile = 'giocatori.json';
$response = [
'success' => false,
'message' => 'Errore sconosciuto.'
];
// Controlla che sia POST
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
$response['message'] = 'Metodo non valido.';
echo json_encode($response);
exit;
}
// Legge il corpo della richiesta JSON
$jsonPayload = file_get_contents('php://input');
$data = json_decode($jsonPayload, true);
if ($data === null) {
$response['message'] = 'Dati JSON non validi.';
echo json_encode($response);
exit;
}
// Funzione per convertire stringhe numeriche in numeri (come nel tuo salva_squadre)
function recursively_convert_numeric_strings(&$arr) {
foreach ($arr as $key => &$value) {
if (is_array($value)) {
recursively_convert_numeric_strings($value);
} elseif (is_string($value) && is_numeric($value)) {
$value = $value + 0;
}
}
}
recursively_convert_numeric_strings($data);
// Salva voti
if (isset($data['voti'])) {
$prettyVoti = json_encode($data['voti'], JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
if (file_put_contents($votiFile, $prettyVoti) === false) {
$response['message'] = 'Errore durante il salvataggio di voti.json';
echo json_encode($response);
exit;
}
}
// Salva giocatori
if (isset($data['giocatori'])) {
$prettyGiocatori = json_encode($data['giocatori'], JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
if (file_put_contents($giocatoriFile, $prettyGiocatori) === false) {
$response['message'] = 'Errore durante il salvataggio di giocatori.json';
echo json_encode($response);
exit;
}
}
$response['success'] = true;
$response['message'] = 'Voti e giocatori salvati con successo!';
echo json_encode($response);
?>