Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get US State from GeoIP2?

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?

like image 236
mvorisek Avatar asked Feb 10 '23 07:02

mvorisek


1 Answers

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'
like image 55
Kristján Avatar answered Feb 11 '23 21:02

Kristján