Search code examples
phpapistreet-addresspostal-codepaf

How do I look up a UK address based on house number and postcode?


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


Solution

  • 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.