鉴于英国的门牌号码和邮政编码,我该如何将其翻译成完整的邮寄地址?
答案 0 :(得分:3)
这非常有效。但是,我认为最后会遗漏一行代码,我已添加:
$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);
?>
也可以使用getaddress.io等商业服务。