我想使用某种API服务来接收邮政编码并返回与此邮政编码匹配的任何城市。
USPS在他们网站上的手动查询就是这样做的,Crate和Barrel在他们的结账时这样做了。
似乎USPS的zipcode API只能返回一个城市。我已经查看了Geonames数据库,但它似乎也设置为每个zip只有一个城市。
有没有办法实现这个目标?
答案 0 :(得分:2)
我会继续发布@Matt的答案,因为我已经将SmartyStreets ZIP Code API用于此目的。您可以通过以下三种方式之一查询此API:
对于单个邮政编码(这是您要问的问题),这是一个简单的GET
请求:
curl -v 'https://us-zipcode.api.smartystreets.com/lookup?
auth-id=YOUR+AUTH-ID+HERE&
auth-token=YOUR+AUTH-TOKEN+HERE&
zipcode=65202'
回复(有效AUTH-ID
和AUTH-TOKEN
)是:
[
{
"input_index": 0,
"city_states": [
{
"city": "Columbia",
"state_abbreviation": "MO",
"state": "Missouri",
"mailable_city": true
},
{
"city": "Hinton",
"state_abbreviation": "MO",
"state": "Missouri",
"mailable_city": false
},
{
"city": "Lindbergh",
"state_abbreviation": "MO",
"state": "Missouri",
"mailable_city": false
},
{
"city": "Midway",
"state_abbreviation": "MO",
"state": "Missouri",
"mailable_city": false
},
{
"city": "Murry",
"state_abbreviation": "MO",
"state": "Missouri",
"mailable_city": false
},
{
"city": "Prathersville",
"state_abbreviation": "MO",
"state": "Missouri",
"mailable_city": false
},
{
"city": "Shaw",
"state_abbreviation": "MO",
"state": "Missouri",
"mailable_city": false
},
{
"city": "Stephens",
"state_abbreviation": "MO",
"state": "Missouri",
"mailable_city": false
}
],
"zipcodes": [
{
"zipcode": "65202",
"zipcode_type": "S",
"default_city": "Columbia",
"county_fips": "29019",
"county_name": "Boone",
"state_abbreviation": "MO",
"state": "Missouri",
"latitude": 38.99775,
"longitude": -92.30798,
"precision": "Zip5",
"alternate_counties": [
{
"county_fips": "29027",
"county_name": "Callaway",
"state_abbreviation": "MO",
"state": "Missouri"
}
]
}
]
}
]
如您所见,这给出了与图片示例相同的城市列表。它还包含有关列表中第一个city_state
的更详细信息。
为了完整性,这是一个演示所有三个查询选项的示例请求。 (n.b.对于单邮政编码之外的任何请求,API需要POST
电话):
curl -v 'https://us-zipcode.api.smartystreets.com/lookup?
auth-id=YOUR+AUTH-ID+HERE&
auth-token=YOUR+AUTH-TOKEN+HERE'
-H "Content-Type: application/json"
--data-binary '
[
{
"zipcode":"12345"
},
{
"city":"North Pole",
"state":"AK"
},
{
"city":"cupertino",
"state":"CA",
"zipcode":"95014"
}
]'
以下是该示例请求的响应:
[
{
"input_index": 0,
"city_states": [
{
"city": "Schenectady",
"state_abbreviation": "NY",
"state": "New York",
"mailable_city": true
},
{
"city": "General Electric",
"state_abbreviation": "NY",
"state": "New York",
"mailable_city": false
},
{
"city": "Schdy",
"state_abbreviation": "NY",
"state": "New York",
"mailable_city": false
}
],
"zipcodes": [
{
"zipcode": "12345",
"zipcode_type": "U",
"default_city": "Schenectady",
"county_fips": "36093",
"county_name": "Schenectady",
"state_abbreviation": "NY",
"state": "New York",
"latitude": 42.81565,
"longitude": -73.94232,
"precision": "Zip5"
}
]
},
{
"input_index": 1,
"city_states": [
{
"city": "North Pole",
"state_abbreviation": "AK",
"state": "Alaska",
"mailable_city": true
}
],
"zipcodes": [
{
"zipcode": "99705",
"zipcode_type": "S",
"default_city": "North Pole",
"county_fips": "02090",
"county_name": "Fairbanks North Star",
"state_abbreviation": "AK",
"state": "Alaska",
"latitude": 64.77911,
"longitude": -147.36885,
"precision": "Zip5"
}
]
},
{
"input_index": 2,
"city_states": [
{
"city": "Cupertino",
"state_abbreviation": "CA",
"state": "California",
"mailable_city": true
}
],
"zipcodes": [
{
"zipcode": "95014",
"zipcode_type": "S",
"default_city": "Cupertino",
"county_fips": "06085",
"county_name": "Santa Clara",
"state_abbreviation": "CA",
"state": "California",
"latitude": 37.32056,
"longitude": -122.03865,
"precision": "Zip5"
}
]
}
]
我希望有所帮助!
编辑 - 我已使用特定于问题的示例对此进行了更新。感谢@AmyAnuszewski打电话给我。