- Lid geworden
- 13 aug 2013
- Berichten
- 5.500
Voor een "postcode naar adres controle" heb ik een scriptje gemaakt. Hiervoor heb ik deze API (info) gebruikt. Voor iedereen die hier iets aan heeft 
Een voorbeeld hoe je de functie gebruikt.
Nog een paar voorbeelden.

PHP:
function postcode2adres ($postc='', $huisnr='', $toevoeg='', $limit=25) {
$_DEFAULT_LIMIT = 25;
$_QUERY = 'straatnaam,huisnummer,huisletter,postcode,wijknaam,woonplaatsnaam,provincieafkorting';
// cleanup
$postc = strtoupper(str_replace(' ', '', $postc));
$huisnr = strtoupper(str_replace(' ', '', $huisnr));
$toevoeg = strtoupper(str_replace(' ', '', $toevoeg));
// validatie
if (preg_match('/^[1-9][0-9]{3}[A-Z]{2}$/', $postc) === false) return false;
if (!is_int($limit)) $limit = $_DEFAULT_LIMIT;
// query url
$api = 'https://geodata.nationaalgeoregister.nl/locatieserver/v3/free';
$api .= '?fq=type:adres&fq=postcode:' . $postc;
if ($huisnr != '') {
$api .= '&fq=huis_nlt:' . $huisnr;
if ($toevoeg == '*') $api .= '*';
elseif ($toevoeg != '') $api .= $toevoeg;
}
$api .= '&fl=' . $_QUERY . '&rows=' . (string) $limit;
// null bij een error (api of json)
try {
$data = file_get_contents($api);
$obj = json_decode($data);
} catch (Exception $e) {
return null;
}
// false als response onjuist is
try {
$numFound = $obj->response->numFound;
$arrOutput = $obj->response->docs;
} catch (Exception $e) {
return false;
}
if ($numFound == 0) return false;
return $arrOutput;
}
Een voorbeeld hoe je de functie gebruikt.
PHP:
$result = postcode2adres ('2712 AL', '36', '*', 25);
if (is_null($result)) {
echo "Gegevens opvragen niet gelukt.";
} elseif (empty($result)) {
echo "Postcode en/of huisnummer(s) niet gevonden.";
} else {
echo '<code><pre>';
print_r($result);
echo '</pre></code>';
}
Nog een paar voorbeelden.
PHP:
// alle huisnummers (standaard max. 25 huisnummers)
$result = postcode2adres ('2712 AL')
// hetzelfde maar dan ander max. aantal huisnummers (hier max. 75)
$result = postcode2adres ('2712 AL', '', '', 75)
// alleen huisnummer 36
$result = postcode2adres ('2712 AL', '36')
// alleen huisnummer 36-C
$result = postcode2adres ('2712 AL', '36', 'C')
// alle huisnummers die bij huisnummer 36 horen
$result = postcode2adres ('2712 AL', '36', '*')