在javascript中使用Postcode Anywhere的地址查找器,需要将密钥放在PHP中

时间:2014-02-28 10:23:53

标签: javascript php ajax postal-code

我目前正在实施从Postcode到我的结账的地址查询。

我已经将它与我的结账工作了,但目前它都是纯JavaScript,我无法真正做到这一点,因为这意味着我的Postcode Anywhere键也在显示。

显然我需要做这个东西的一部分服务器端(在我的情况下使用PHP)。但我不太确定它有多少以及如何连接。我在这里围绕PCA_Find_By_Parts.html JavaScript示例构建了我当前的解决方案:

http://www.postcodeanywhere.co.uk/support/sample-code.aspx

但对我来说,这似乎不太安全,因为我提到钥匙正在显示。

1 个答案:

答案 0 :(得分: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) {}
    }
});