How can I get US State from GeoIP2?
Asked Answered
W

1

6

Is there any way, how to get US state abbr. (CA, VI, ..) from IP?

The library return me only metro code and postal code, is there any lookup table to state convertor? Or how do you extract the state?

Williamson answered 16/8, 2015 at 18:28 Comment(0)
P
11

You need to look in the subdivisions section, which is the generic container for States, Provinces, etc. depending on the country.

If you're using the web API, here are those docs, and this is the relevant chunk from their output example:

"subdivisions":  [
    {
        "confidence":  50,
        "geoname_id":  5332921,
        "iso_code":    "CA",
        "names":  {
            "de":    "Kalifornien",
            "en":    "California",
            "es":    "California",
            "fr":    "Californie",
            "ja":    "カリフォルニア",
            "ru":    "Калифорния",
            "zh-CN": "加州"
        }
    }
]

The PHP client using a downloaded IP DB has mostSpecificSubdivision:

print($record->mostSpecificSubdivision->name . "\n"); // 'Minnesota'
print($record->mostSpecificSubdivision->isoCode . "\n"); // 'MN'
Possessive answered 16/8, 2015 at 18:43 Comment(1)
Thank you, accepted as answer, the subdivisions are the best.Williamson

© 2022 - 2024 — McMap. All rights reserved.