Created
April 13, 2010 10:08
-
-
Save pezholio/364477 to your computer and use it in GitHub Desktop.
A helper function for UK Postcodes, adapted from the original function I wrote for the Ernest Marples service
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function ernest_marples($postcode) { | |
$postcode = str_replace(" ", "", $postcode); | |
$url = "http://www.uk-postcodes.com/postcode/". urlencode($postcode) .".csv"; // Build the URL | |
$file = file_get_contents($url); | |
if (strpos($file, "html") === FALSE) { // Some error checking - if the file contains html, then we've been redirected to the homepage and something has gone wrong | |
$pieces = explode(",", $file); | |
$result['postcode'] = $pieces[0]; | |
$result['lat'] = $pieces[1]; | |
$result['lng'] = $pieces[2]; | |
} else { | |
$result['error'] = TRUE; // If an error, return one | |
} | |
return $result; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment