在javascript中使用Postcode Anywhere的地址查找器,需要将密钥放在PHP中
Posted
技术标签:
【中文标题】在javascript中使用Postcode Anywhere的地址查找器,需要将密钥放在PHP中【英文标题】:Address finder using Postcode Anywhere in javascript, need to put the key in PHP 【发布时间】:2014-02-28 10:23:52 【问题描述】:我目前正在执行从邮政编码到结帐的地址查找。
我已经让它与我的结帐一起使用,但目前它都是纯 javascript,我真的不能这样做,因为这意味着我的 Postcode Anywhere 密钥也在显示中。
很明显,我需要在服务器端做这件事的一部分(在我的例子中是 php)。但我不太确定这一切有多少以及如何联系起来。我在这里围绕PCA_Find_By_Parts.html
JavaScript 示例构建了我当前的解决方案:
http://www.postcodeanywhere.co.uk/support/sample-code.aspx
但对我来说,这似乎不是很安全,正如我提到的那样,钥匙正在展示中。
【问题讨论】:
【参考方案1】:您将希望在后端 (PHP) 上进行所有“处理”,并使用 Ajax 函数传递相关数据。
这里有一个很好的例子:
https://www.postcodeanywhere.co.uk/address-validation/guide/default.aspx?reg=1
网站上有 PHP 示例:
http://downloads.postcodeanywhere.co.uk/dev/pcaphpsamples.zip
http://downloads.postcodeanywhere.co.uk/dev/pcaphpsamples.zip
http://downloads.postcodeanywhere.co.uk/dev/phpSOAP.zip
您还可以使用以下代码开始:
class PostcodeAnywhere_Interactive_FindByPostcode_v1_00
//Credit: Thanks to Stuart Sillitoe (http://stu.so/me) for the original PHP that these samples are based on.
private $Key; //The key to use to authenticate to the service.
private $Postcode; //The postcode to search with find.
private $UserName; //The username associated with the Royal Mail license (not required for click licenses).
private $Data; //Holds the results of the query
function PostcodeAnywhere_Interactive_FindByPostcode_v1_00($Key, $Postcode, $UserName)
$this->Key = $Key;
$this->Postcode = $Postcode;
$this->UserName = $UserName;
function MakeRequest()
$url = "http://services.postcodeanywhere.co.uk/PostcodeAnywhere/Interactive/FindByPostcode/v1.00/xmla.ws?";
$url .= "&Key=" . urlencode($this->Key);
$url .= "&Postcode=" . urlencode($this->Postcode);
$url .= "&UserName=" . urlencode($this->UserName);
//Make the request to Postcode Anywhere and parse the XML returned
$file = simplexml_load_file($url);
//Check for an error, if there is one then throw an exception
if ($file->Columns->Column->attributes()->Name == "Error")
throw new Exception("[ID] " . $file->Rows->Row->attributes()->Error . " [DESCRIPTION] " . $file->Rows->Row->attributes()->Description . " [CAUSE] " . $file->Rows->Row->attributes()->Cause . " [RESOLUTION] " . $file->Rows->Row->attributes()->Resolution);
//Copy the data
if ( !empty($file->Rows) )
foreach ($file->Rows->Row as $item)
$this->Data[] = array('Id'=>$item->attributes()->Id,'StreetAddress'=>$item->attributes()->StreetAddress,'Place'=>$item->attributes()->Place);
function HasData()
if ( !empty($this->Data) )
return $this->Data;
return false;
//Example usage
//-------------
//$pa = new PostcodeAnywhere_Interactive_FindByPostcode_v1_00 ("AA11-AA11-AA11-AA11","WR2 6NJ","David");
//$pa->MakeRequest();
//if ($pa->HasData())
//
// $data = $pa->HasData();
// foreach ($data as $item)
//
// echo $item["Id"] . "<br/>";
// echo $item["StreetAddress"] . "<br/>";
// echo $item["Place"] . "<br/>";
//
//
http://www.postcodeanywhere.co.uk/support/webservices/PostcodeAnywhere/Interactive/FindByPostcode/v1/default.aspx
在需要时使用 ajax 进行调用:
jQuery.ajax(
url: "yourScript.php",
type: 'POST',
data: action: 'get_postcode', id: ui.item.id ,
success: function(data)
try
var response = jQuery.parseJSON(data);
jQuery('#pickPostcode').val(response.postcode);
catch(err)
);
【讨论】:
嗨,是的,我已经厌倦了这些,但它们似乎都围绕着做一个我不能做的表单请求,因为邮政编码查找是在一个表单中,你不应该有一个表单一个表格内。我希望有一个 ajax 类型的解决方案,但我不知道该怎么做? @David:这是 AJAX 解决方案。如果您不想使用它,它不需要使用表单。 Opps 抱歉没有看到 jquery 位,谢谢尝试一下!以上是关于在javascript中使用Postcode Anywhere的地址查找器,需要将密钥放在PHP中的主要内容,如果未能解决你的问题,请参考以下文章
如何调整此 javascript 以返回少了最后 2 个字符的邮政编码? [复制]
Pandas - 使用 PostCoder 在每一行中查找纬度和经度,然后在新列中返回 Postcode