64 lines
2.0 KiB
PHP
Executable File
64 lines
2.0 KiB
PHP
Executable File
|
|
<?php
|
|
function getCitiesFromPostalCode($postalCode) {
|
|
$url = "https://apicarto.ign.fr/api/codes-postaux/communes/{$postalCode}";
|
|
$response = file_get_contents($url);
|
|
|
|
if ($response !== false) {
|
|
$data = json_decode($response, true); // Décoder la réponse JSON
|
|
if (is_array($data) && count($data) > 0) {
|
|
$cities = array_column($data, 'nomCommune'); // Récupérer tous les noms de communes
|
|
return $cities; // Retourne un tableau des noms de communes
|
|
}
|
|
}
|
|
return []; // Retourne un tableau vide en cas d'erreur
|
|
}
|
|
|
|
$result = null;
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$postalCode = $_POST['postal_code'] ?? '';
|
|
|
|
if ($postalCode !== '') {
|
|
$cities = getCitiesFromPostalCode($postalCode);
|
|
if (!empty($cities)) {
|
|
$result = $cities; // Stocker toutes les villes retournées
|
|
} else {
|
|
$result = "Impossible de récupérer les villes associées.";
|
|
}
|
|
} else {
|
|
$result = "Veuillez remplir tous les champs correctement.";
|
|
}
|
|
}
|
|
?>
|
|
|
|
<!DOCTYPE html>
|
|
<html lang="fr">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Convertisseur de code postal</title>
|
|
</head>
|
|
<body>
|
|
<h1>Convertisseur de code postal</h1>
|
|
<form method="POST">
|
|
<label for="postal_code">Code postal :</label>
|
|
<input type="text" id="postal_code" name="postal_code" required>
|
|
<button type="submit">Convertir</button>
|
|
</form>
|
|
|
|
<?php if ($result !== null): ?>
|
|
<h2>Résultat :</h2>
|
|
<?php if (is_array($result)): ?>
|
|
<p>Les communes correspondant au code postal sont :</p>
|
|
<ul>
|
|
<?php foreach ($result as $city): ?>
|
|
<li><?= htmlspecialchars($city, ENT_QUOTES, 'UTF-8') ?></li>
|
|
<?php endforeach; ?>
|
|
</ul>
|
|
<?php else: ?>
|
|
<p><?= htmlspecialchars($result, ENT_QUOTES, 'UTF-8') ?></p>
|
|
<?php endif; ?>
|
|
<?php endif; ?>
|
|
</body>
|
|
</html>
|