4

我想使用某种 API 服务来获取邮政编码并返回与该邮政编码匹配的任何城市。

USPS 在他们的网站上进行手动查找,Crate and Barrel 在他们的结账时这样做。

不过,似乎 USPS 的邮政编码 API 只能返回一个城市。我查看了 Geonames 数据库,但它似乎也为每个 zip 设置了一个城市。

有没有办法做到这一点?

Crate and Barrel 的结帐邮政编码自动完成

4

1 回答 1

3

我将继续发布@Matt 的答案,因为我已经为此目的使用了SmartyStreets 邮政编码 API。您可以通过以下三种方式之一查询此 API:

  1. 邮政编码
  2. 城市+州
  3. 城市 + 州 + 邮政编码

对于单个邮政编码(这是您要询问的),这是一个简单的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-IDand 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列表中第一个的更详细信息。

为了完整起见,这里是一个示例请求,演示了所有三个查询选项。(nb 对于单个邮政编码之外的任何请求,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 指出这一点。

于 2018-05-04T19:59:18.003 回答