Skip to content

Instantly share code, notes, and snippets.

@tilnothing
Last active September 13, 2019 04:10
Show Gist options
  • Select an option

  • Save tilnothing/21c9da1e9490d5f0167d97e27427c0dc to your computer and use it in GitHub Desktop.

Select an option

Save tilnothing/21c9da1e9490d5f0167d97e27427c0dc to your computer and use it in GitHub Desktop.
<?php
function lookuppostcode($postcode) {
$postcode = str_replace(" ", "", $postcode);
$key = 'YOUR_API_KEY';
$request = 'https://api.getAddress.io/v2/uk/' . $postcode . '?api-key=' . $key;
$response = file_get_contents($request);
$response = json_decode($response, true);
$returnData = array();
$returnData['lat'] = $response['Latitude'];
$returnData['lon'] = $response['Longitude'];
$returnData['totalAddresses'] = count($response['Addresses']);
$addressArray = explode(',', $response['Addresses'][0]);
$returnData['locality'] = $addressArray[4];
$returnData['city'] = $addressArray[5];
$returnData['county'] = $addressArray[6];
$returnData['addresses'] = array();
foreach ($response['Addresses'] as $address)
{
$addressArray = explode(',', $address);
$fullAddress = '';
foreach ($addressArray as $item){
if($item!=' '){
$fullAddress .= $item . "\n";
}
}
$fullAddress = trim($fullAddress, '\n');
$returnData['addresses'][] = array(
'line1' => $addressArray[0],
'line2' => $addressArray[1],
'line3' => $addressArray[2],
'line4' => $addressArray[3],
'locality' => $addressArray[4],
'city' => $addressArray[5],
'county' => $county[6],
'fullAddress' => $fullAddress,
);
}
return $returnData;
}
//Example usage:
$data = lookuppostcode('nn13er');
echo "<b>Latitude: </b>" . $data['lat'] . "</br>\n";
echo "<b>Longitude: </b>" . $data['lon'] . "</br>\n";
echo "<b>Locality: </b>" . $data['locality'] . "</br>\n";
echo "<b>City: </b>" . $data['city'] . "</br>\n";
echo "<b>County: </b>" . $data['county'] . "</br>\n";
echo "<b>Number of addresses returned: </b>" . $data['totalAddresses'] . "</br>\n";
echo "</br>\n";
echo '<b>Addresses: </b>'."</br>\n";
$i = 1;
foreach($data['addresses'] as $address)
{
echo "<b>Address No" . $i . "</b>" ."</br>\n";
echo "Line 1: " . $address['line1'] ."</br>\n";
echo "Line 2: " . $address['line2'] ."</br>\n";
echo "Line 3: " . $address['line3'] ."</br>\n";
echo "Line 4: " . $address['line4'] ."</br>\n";
echo "Locality: " . $address['locality'] ."</br>\n";
echo "City: " . $address['city'] ."</br>\n";
echo "County: " . $address['county'] ."</br>\n";
echo "Full Address: " . $address['fullAddress'] ."</br>\n";
$i++;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment