Skip to content
Clear Postcode

Integration · Code

UK postcode lookup in PHP

A cURL GET with your key returns everything about a postcode as JSON; decode it and read data.

Get a free key10,000 lookups a month free · no card

Set it up

  1. 1Get a free key at /developers.
  2. 2Store it in an environment variable.
  3. 3Call the endpoint.

The code

$ch = curl_init('https://api.clearpostcode.co.uk/v1/postcodes/' . rawurlencode('SW1A 1AA'));
curl_setopt_array($ch, [
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_HTTPHEADER => ['x-api-key: ' . getenv('CLEAR_KEY')],
  CURLOPT_TIMEOUT => 5,
]);
$d = json_decode(curl_exec($ch), true)['data'] ?? null;
echo $d ? $d['local_authority']['name'] : 'Not found';