通过提供邮政编码获取城市名称的 Web 服务 [英] Webservice to get City Names by giving Zip Codes

查看:23
本文介绍了通过提供邮政编码获取城市名称的 Web 服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个可靠的网络服务,它通过传递邮政编码给出相应的城市名称.此网络服务应随时可用.该网络服务也将用于生产.

I need a reliable webserivce which gives corresponding city name by passing zip code. This webservice should work at any time. This webservice will be used in the production also.

推荐答案

我找到了几种使用基于 Web 的 API 来实现此目的的方法.我认为 美国邮政服务 将是最准确的,因为邮政编码是他们的东西,但是 Ziptastic 看起来容易多了.

I found a couple of ways to do this with web based APIs. I think the US Postal Service would be the most accurate, since Zip codes are their thing, but Ziptastic looks much easier.

根据此美国邮政服务网站上记录了基于 XML 的 Web API 的页面,特别是 此 PDF 文档的第 4.0 节(第 22 页),他们有您可以在其中发送包含 5 位邮政编码的 XML 请求的 URL,他们将使用包含相应城市和州的 XML 文档进行响应.

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:

http://SERVERNAME/ShippingAPITest.dll?API=CityStateLookup&XML=<CityStateLookupRequest%20USERID="xxxxxxx"><ZipCode ID= "0"><Zip5>90210</Zip5></ZipCode></CityStateLookupRequest>

以下是您将收到的回复:

And here's what you would receive back:

<?xml version="1.0"?> 
<CityStateLookupResponse> 
    <ZipCode ID="0"> 
        <Zip5>90210</Zip5> 
        <City>BEVERLY HILLS</City> 
        <State>CA</State> 
    </ZipCode> 
</CityStateLookupResponse>

USPS 确实要求您在使用 API 之前向他们注册,但据我所知,访问是免费的.顺便说一下,他们的 API 有一些其他功能:您可以进行地址标准化和邮政编码查找,以及一整套跟踪、运输、标签等.

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.

这是一项相当新的服务,但根据他们的文档,您只需向 http://ziptasticapi 发送 GET 请求即可.com,像这样:

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:

GET http://ziptasticapi.com/48867

他们将按照以下方式返回一个 JSON 对象:

And they will return a JSON object along the lines of:

{"country": "US", "state": "MI", "city": "OWOSSO"}

确实有效.您可以通过执行以下操作从命令行进行测试:

Indeed, it works. You can test this from a command line by doing something like:

curl http://ziptasticapi.com/48867 

这篇关于通过提供邮政编码获取城市名称的 Web 服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆