4

ある種の API サービスを使用して郵便番号を取得し、この郵便番号に一致する都市を返したいと考えています。

サイトでの USPS 手動ルックアップはこれを行い、Crate and Barrel はチェックアウトでこれを行います。

ただし、USPS の zipcode API は 1 つの都市しか返せないようです。Geonames データベースを調べましたが、zip ごとに 1 つの都市のみに設定されているようです。

これを達成する方法はありますか?

Crate and Barrel のチェックアウトの郵便番号オートコンプリート

4

1 に答える 1

3

SmartyStreets ZIP Code APIをまさにその目的で使用したので、@Matt の回答を投稿します。この API を照会するには、次の 3 つの方法のいずれかを使用できます。

  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-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ています。

完全を期すために、3 つのクエリ オプションすべてを示すサンプル リクエストを次に示します。(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 に答える