我应该如何根据邮政编码填充城市/州字段?
我知道有邮政编码数据库,但是我如何根据该数据库获取城市/州字段?这些数据库是否包含城市/州,或者我是否必须对网络服务进行某种查找?
I'm aware there are databases for zip codes, but how would I grab the city/state fields based on that? Do these databases contain the city/states or do I have to do some sort of lookup to a webservice?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
\begin{been-there-done-that}
重要的认识:城市/县和邮政编码之间不存在一对一的映射。邮政编码不是基于政治区域,而是基于为 USPS 内部使用定义的配送区域。除非您有 +4 或整个街道地址来匹配 USPS 地址数据库中的记录,否则根据邮政编码查找城市是没有意义的;否则,您将不知道是 RICHMOND 还是 HENRICO、DALLAS 还是 FORT WORTH,因为没有足够的信息来区分。
例如,这就是为什么许多电子商务供应商发现处理纽约州销售税令人沮丧,因为该税收计划是基于县的,电子商务系统通常不会询问县和邮政编码(唯一的信息)。相反,他们提供的信息)在纽约可以跨越县界。
美国邮政局每月都会更新其地址数据库,并且需要花费大量资金,因此您在互联网上找到的免费列表几乎都会过时,特别是在美国邮政局为了省钱而关闭邮局的情况下。
一个邮政编码可能跨越多个地名,一个城市经常使用多个(但不一定是整个)邮政编码。最后,邮政编码文件中列出的城市名称实际上可能并不代表收件人实际居住的地方;相反,它代表了他们邮局的位置。我们的办公室邮件寄往阿什兰,但我们的工作地点距离该镇的实际政治限制大约 7 英里。亚什兰恰好是我们承运商航线的始发地。
对于猜测某人的位置(例如搜索附近的兴趣点),这些来源和城市/州/邮政编码集可能没问题,但不需要精确。但是对于数据输入场景中的地址验证呢?绝对不行——验证整个地址,或者根本不用担心。
只是友好提醒您退后一步并记住数据源的预期用途!
\end{已经完成了那件事}
\begin{been-there-done-that}
Important realization: There is not a one-to-one mapping between cities/counties and ZIP codes. A ZIP code is not based on a political area but instead a distribution area as defined for the USPS's internal use. It doesn't make sense to look up a city based on a ZIP code unless you have the +4 or the entire street address to match a record in the USPS address database; otherwise, you won't know if it's RICHMOND or HENRICO, DALLAS or FORT WORTH, there's just not enough information to tell.
This is why, for example, many e-commerce vendors find dealing with New York state sales tax frustrating, since that tax scheme is based on county, e-commerce systems typically don't ask for the county, and ZIP codes (the only information they provide instead) in New York can span county lines.
The USPS updates its address database every month and costs real money, so pretty much any list that you find freely available on the Internet is going to be out of date, especially with the USPS closing post offices to save money.
One ZIP code may span multiple place names, and one city often uses several (but not necessarily whole) ZIP codes. Finally, the city name listed in the ZIP code file may not actually be representative of the place in which the addressee actually lives; instead, it represents the location of their post office. Our office mail is addressed to ASHLAND, but we work about 7 miles from the town's actual political limits. ASHLAND just happens to be where our carrier's route originates from.
For guesstimating someone's location, such as for a search of nearby points of interest, these sources and City/State/ZIP sets are probably fine, they don't need to be exact. But for address validation in a data entry scenario? Absolutely not--validate the whole address or don't bother at all.
Just a friendly reminder to take a step back and remember the data source's intended use!
\end{been-there-done-that}
现代邮政编码数据库包含城市、州字段字段。
http://sourceforge.net/projects/zips/
http://www.populardata.com/
Modern zip code databases contain columns for City, State fields.
http://sourceforge.net/projects/zips/
http://www.populardata.com/
使用 Ziptastic HTTP/JSON API
这是一项相当新的服务,但根据他们的文档,它看起来像是您所需要的一切要做的就是向 http://ziptasticapi.com 发送 GET 请求,如下所示:
他们将返回 JSON 对象的思路是:
确实,它有效。您可以通过执行以下操作从命令行测试这一点:
Using the Ziptastic HTTP/JSON API
This is a pretty new service, but according to their documentation, it looks like all you need to do is send a GET request to http://ziptasticapi.com, like so:
And they will return a JSON object along the lines of:
Indeed, it works. You can test this from a command line by doing something like:
使用美国邮政服务 HTTP/XML API
根据此美国邮政服务网站上的页面,该页面记录了其基于 XML 的 Web API,特别是 此 PDF 文档,他们有一个 URL,您可以在其中发送 XML 请求包含 5 位邮政编码,他们将使用包含相应城市和州的 XML 文档进行响应。
根据他们的文档,您将发送以下内容:
以下是您将收到的返回内容:
USPS 确实要求您在使用 API 之前向他们注册,但是,据我所知,访问无需付费。顺便说一句,他们的 API 还有一些其他功能:您可以进行地址标准化和邮政编码查找,以及整套跟踪、运输、标签等。
Using the US Postal Service HTTP/XML API
According to this page on the US Postal Service website which documents their XML based web API, specifically Section 4.0 (page 22) of this PDF document, they have a URL where you can send an XML request containing a 5 digit Zip Code and they will respond with an XML document containing the corresponding City and State.
According to their documentation, here's what you would send:
And here's what you would receive back:
USPS does require that you register with them before you can use the API, but, as far as I could tell, there is no charge for access. By the way, their API has some other features: you can do Address Standardization and Zip Code Lookup, as well as the whole suite of tracking, shipping, labels, etc.
我将尝试回答“我应该如何填充...”的问题,而不是“我应该填充...”
假设您要多次执行此操作,您会想要构建自己的数据库。这可能只不过是您从许多来源下载的文本文件(请参阅此处的 Pentium10 回复)。当您需要城市名称时,您可以搜索邮政编码,并提取城市/州文本。为了加快速度,您可以按 ZIP 按数字顺序对表进行排序,构建行索引,并使用二分搜索。
如果您的 ZIP 数据库看起来像(来自sourceforge):
最简单从文本中提取内容会类似于
如果您只是在 PHP 中处理文本,那么这就是您所需要的。但如果您有数据库应用程序,您将使用 SQL 完成所有操作。有关您申请的更多详细信息可能会引起更详细的答复。
I'll try to answer the question "HOW should I populate...", and not "SHOULD I populate..."
Assuming you are going to do this more than once, you would want to build your own database. This could be nothing more than a text file you downloaded from any of the many sources (see Pentium10 reply here). When you need a city name, you search for the ZIP, and extract the city/state text. To speed things up, you would sort the table in numeric order by ZIP, build an index of lines, and use a binary search.
If you ZIP database looked like (from sourceforge):
The most simple-minded extraction from the text would go something like
If you are just playing with text in PHP, that's all you need. But if you have a database application, you would do everything in SQL. Further details on your application may elicit more detailed responses.