esp/api/add_entidad.php

18 lines
829 B
PHP

<?php
header('Content-Type: application/json');
$data = json_decode(file_get_contents('php://input'), true);
if (isset($data['entidad'])) {
$entidad = $data['entidad'];
$path = __DIR__ . '/../data/entidades.json';
$json = file_exists($path) ? file_get_contents($path) : '{"entidades":[]}';
$entidades = json_decode($json, true);
if (!is_array($entidades)) { $entidades = ['entidades' => []]; }
if (!isset($entidades['entidades']) || !is_array($entidades['entidades'])) { $entidades['entidades'] = []; }
$entidades['entidades'][] = $entidad;
file_put_contents($path, json_encode($entidades, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
echo json_encode(['success' => true]);
} else {
echo json_encode(['success' => false, 'message' => 'No se proporcionó entidad.']);
}