1

Given a house number and postcode in the UK, how can I translate this into a full mailing address?

Emil Borconi
  • 3,326
  • 2
  • 24
  • 40
  • 3
    I think the downvotes may be because the format of the question is off-topic - it is expressed as a requirement for an external third-party resource. I am wondering if there is a way to reword it so it is on-topic. Thanks for the contribution, though! – halfer Apr 13 '16 at 20:33
  • Yeah, the title of the question sounds spammy. – Aaron Gillion Apr 13 '16 at 20:35
  • Ok, got it. I have edited the title and the questions, so it should be clear that is a tutorial not a question looking for somebody to write me the code. – Emil Borconi Apr 13 '16 at 20:49
  • I shortened it up and voted to reopen, I think it will be a more acceptable format now. – Mark Harrison Apr 13 '16 at 22:16
  • @EmilBorconi The title still looked like spam after your edit - "free" and lots of caps scream "spam". Also, whining about down votes tends to attract more down votes. – nobody Apr 13 '16 at 22:23
  • Thank you both for the help. – Emil Borconi Apr 13 '16 at 22:54

1 Answers1

3

This works really well. However, I think there is a line of code missing at the end, which I have added:

$data1 = json_decode(curl_exec($ch),true);

The UK Postal Service website allows 50 address lookups per day. Here's a PHP example of how it is called.

<?php
$house=$_GET['house'];
$postcode=$_GET['postcode'];
$ch =  curl_init();
//We need to get a KEY.
curl_setopt($ch, CURLOPT_URL, "www.royalmail.com/rml-shared-find-a-postcode");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$data = curl_exec($ch);
preg_match("/key\":\"([a-zA-Z0-9-]*)/",$data,$key);
$key=$key[1];
//Now that we have the key, we just need to make a simple request.
curl_setopt($ch, CURLOPT_URL, "http://api.postcodefinder.royalmail.com/CapturePlus/Interactive/Find/v2.00/json3ex.ws?Key=$key&Country=GBR&SearchTerm=".urlencode($postcode.",".$house).'&LanguagePreference=en&LastId=&SearchFor=Everything&$block=true&$cache=true');
$data = json_decode(curl_exec($ch),true);
//We can stop here or if we want to get the fully formatted address we go one more query.
$locationid=urlencode($data['Items'][0]['Id']);
curl_setopt($ch, CURLOPT_URL, "http://api.postcodefinder.royalmail.com/CapturePlus/Interactive/RetrieveFormatted/v2.00/json3ex.ws?Key=".$key."&Id=$locationid");

$data = json_decode(curl_exec($ch),true);

print_r($data);
?>

UPDATE 2019 Above method have stopped working as there where some changes on the RoyalMail Website, so here is the updated code:

<?php
$house=$_POST['house'];
$postcode=$_POST['postcode'];
$ch =  curl_init();

curl_setopt($ch, CURLOPT_URL, "www.royalmail.com/rml-shared-find-a-postcode");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Origin: "https://www.royalmail.com"',
    'Referer: "https://www.royalmail.com//rml-shared-find-a-postcode"'
));
$data = curl_exec($ch);
preg_match("/key\":\"([a-zA-Z0-9-]*)/",$data,$key);
$key=$key[1];
curl_close ($ch);



$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "https://services.postcodeanywhere.co.uk/Capture/Interactive/Find/v1.00/json3ex.ws?Key={$key}&Text=".urlencode($postcode.",".$house)."&Origin=GBR&Language=en&Container=&Filter=undefined&Instance=null&Test=false&block=true&cache=true");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');

curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');

$headers = array();
$headers[] = 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36';
$headers[] = 'Referer: https://www.royalmail.com/rml-shared-find-a-postcode';
$headers[] = 'Origin: https://www.royalmail.com';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$data = json_decode(curl_exec($ch),true);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}

//die("https://services.postcodeanywhere.co.uk/Capture/Interactive/Find/v1.00/json3ex.ws?Key=$key&Origin=GBR&Text=".urlencode($postcode.",".$house).'&Language=en&LastId=&SearchFor=Everything&$block=true&$cache=true');
//$data = json_decode(curl_exec($ch),true);

$locationid=urlencode($data['Items'][0]['Id']);

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "https://services.postcodeanywhere.co.uk/Capture/Interactive/Retrieve/v1.00/json3ex.ws?Key={$key}&Id={$locationid}&Source=&cache=true&field1format=%7BLatitude%7D&field2format=%7BLongitude%7D&field3format=%7BBFPONumber%7D");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');

curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');

$headers = array();
$headers[] = 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36';
$headers[] = 'Referer: https://www.royalmail.com/rml-shared-find-a-postcode';
$headers[] = 'Origin: https://www.royalmail.com';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$data = json_decode(curl_exec($ch),true);

curl_close ($ch);



$my_array=array();
$my_array['address1']=$data['Items'][0]['Line1'];
$my_array['address2']=$data['Items'][0]['Line2'].",".$data['Items'][0]['Line3'].",".$data['Items'][0]['Line4'].",".$data['Items'][0]['Line5'];
$my_array['city']=$data['Items'][0]['City'];
$my_array['zip']=$data['Items'][0]['PostalCode'];
echo json_encode($my_array);
?>

Commercial services such as getaddress.io can also be used.

Emil Borconi
  • 3,326
  • 2
  • 24
  • 40