FindMyNearest

Downloads

A bit about FindMyNearest

FindMyNearest is a PHP class for working with UK postcodes, and in particular for finding the distance between two postcodes. FindMyNearest is at the heart of geopostcode.org.uk. It was originally written as part of a project to help people find a local tree surgeon, hence the name.

FindMyNearest can be used with geopostcode.org.uk to retrieve and work with UK postcode data. It also supports several other web postcode services, and storing data locally in either MySQL or a delimited text file.

Using FindMyNearest with geopostcode.org.uk

FindMyNearest comes with a driver for accessing data via geopostcode.org.uk's API. It caches results, helping to reduce load on our server. Here's a quick synopsis:

<?php
require 'path/to/FindMyNearest.php';

// cachefile must be readable and writeable
// please set cachettl to at least 604800 seconds (one week)
$fmn FindMyNearest::factory('geopostcode', array(
  
'cachefile' => 'path/to/cachefile',
  
'cachettl' => 604800)
);

// loaddata must always be called before any method
// that needs to look up data
if (! $fmn->loaddata()) {
  echo 
"Error loading data: " $fmn->lasterror() . "\n";
  exit;
}

// Check if we know about a code. Returns the best match, or false if no match
$goodcode $fmn->validcode('SW1A 1AA');    

// calculate distance between two codes
$distance $fmn->calc_distance('SW1A 1AA''CV32 5HZ'); 

if (
$distance === false) {
  echo 
"There was an error " $fmn->lasterror();
}

// 'normalise' a postcode
$normalised $fmn->normalise('sw1A1aA');
// $normalised is now set to SW1A 1AA

// Are two postcodes the same?
$same $fmn->samepostcode('SW1A 1AA''sw1A1aA');
?>  

More documentation is available in FindMyNearest's README file or on the FindMyNearest wiki.

Obtaining FindMyNearest

Two versions of FindMyNearest are made available here. The standard version includes an SQL file to create a local MySQL table containing data from Ordnance Survey's Code Point Open. This makes it a big file. We also distribute a version without the SQL file. If you plan to use FindMyNearest with geopostcode.org.uk or another web service you won't need the SQL file.

FindMyNearest is developed on Git-Hub, from where you can always get the latest source: FindMyNearest on Git-hub.