54 lines
1.1 KiB
PHP
54 lines
1.1 KiB
PHP
<?php declare(strict_types=1);
|
|
|
|
function getParam(string $name, string $default = ""): string {
|
|
if (isset($_GET[$name]) && !empty($_GET[$name])) {
|
|
return (string)$_GET[$name];
|
|
}
|
|
|
|
return $default;
|
|
}
|
|
|
|
function getParamInt(string $name, int $default = 0): int {
|
|
if (isset($_GET[$name]) && !empty($_GET[$name])) {
|
|
return (int)$_GET[$name];
|
|
}
|
|
|
|
return $default;
|
|
}
|
|
|
|
$zip = getParamInt("zip", 0);
|
|
$city = getParam("city", "");
|
|
$time = time();
|
|
|
|
$url = "https://service.post.ch/zopa/app/api/addresschecker/v1/zips?limit=9999&noCache={$time}";
|
|
|
|
if (strlen($city) > 0) {
|
|
$url .= "&city={$city}";
|
|
}
|
|
|
|
if ($zip >= 1000 && $zip <= 9999) {
|
|
$url .= "&zip={$zip}";
|
|
}
|
|
|
|
$response = file_get_contents($url);
|
|
$json = json_decode($response);
|
|
|
|
$zips = [];
|
|
if (isset($json->zips) && !empty($json->zips)) {
|
|
foreach ($json->zips as $zip) {
|
|
if (!isset($zip->zip) || empty($zip->zip)) {
|
|
continue;
|
|
}
|
|
|
|
if (!isset($zip->city27) || empty($zip->city27)) {
|
|
continue;
|
|
}
|
|
|
|
$zips[(int)$zip->zip] = $zip->city27;
|
|
}
|
|
}
|
|
|
|
header("Content-Type: application/json");
|
|
echo json_encode([
|
|
"zips" => $zips
|
|
], JSON_PRETTY_PRINT);
|