Getting a user country name from originating IP address with Ruby on Rails

前端 未结 11 1685
一整个雨季
一整个雨季 2020-12-04 08:26

I want to extract a user country name from visitors\' IP addresses.

I could get the IP address with remote_ip. But what could be the easiest way to get

11条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-04 08:44

    One service you could use to do this is my own, https://ipinfo.io. It gives you country code and a bunch of other details:

    $ curl ipinfo.io
    {
      "ip": "24.6.61.239",
      "hostname": "c-24-6-61-239.hsd1.ca.comcast.net",
      "city": "Mountain View",
      "region": "California",
      "country": "US",
      "loc": "37.3845,-122.0881",
      "org": "AS7922 Comcast Cable Communications, LLC",
      "postal": "94040"
    }
    

    If you just want the country you can get that by requesting /country

    $ curl ipinfo.io/country
    US
    

    You can then map from a country code to a name using the data from http://country.io, or using the example at http://ipinfo.io/developers/full-country-names

提交回复
热议问题