如何根据门牌号和邮政编码查找英国地址?

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

给定英国的门牌号码和邮政编码,我如何将其转换为完整的邮寄地址?

这真的很好用。但是,我认为最后缺少一行代码,我已经添加了:

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

英国邮政服务网站每天允许 50 次地址查询。 这是如何调用的 PHP 示例。

<?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);
?>

2019 年更新 上述方法已停止工作,因为 RoyalMail 网站上有一些更改,因此这是更新后的代码:

<?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);
?>

也可以使用getaddress.io等商业服务。