【问题标题】:How do I look up a UK address based on house number and postcode? [closed]如何根据门牌号和邮政编码查找英国地址? [关闭]
【发布时间】:2016-04-13 20:18:40
【问题描述】:

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

【问题讨论】:

  • 我认为反对意见可能是因为问题的格式是题外话 - 它被表达为对外部第三方资源的要求。我想知道是否有办法改写它,使其成为主题。不过,感谢您的贡献!
  • 是的,这个问题的标题听起来很垃圾。
  • 好的,知道了。我已经编辑了标题和问题,所以应该清楚的是,这是一个教程,而不是一个找人给我写代码的问题。
  • 我缩短了它并投票重新开放,我认为它现在将是一种更容易接受的格式。
  • @EmilBorconi 编辑后标题看起来仍然像垃圾邮件 - “免费”,而且很多大写字母都在尖叫“垃圾邮件”。此外,抱怨反对票往往会吸引更多反对票。

标签: php api street-address postal-code paf


【解决方案1】:

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

$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等商业服务。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-03
    • 1970-01-01
    • 2016-07-20
    相关资源
    最近更新 更多