Turn IP number into location

Asked

Viewed 187 times

1

I get a file on .csv with data from some users.

NOME | E-MAIL | IP | DATA

There’s no location on it. I wonder if there’s any way I can get the IP number and turn it into the location.

There are services on the internet that inform the location when an IP is informed. The difference is that I own more than one IP number, it will be a list.

I have no idea how to do that, and I don’t know if that’s possible. I thought I’d make a html with a input, where after I upload this file, it did this conversion. I thought of something Javascript.

  • 3
  • @Bacco No. Yeah, I’d like to know how to convert a list.

  • 1

    It would be the case to focus the question on what you want to know. If you don’t know how to iterate and extract the IP from the line, ask this. If the problem is how to call the API inside the loop, show the API you chose and your attempt. Here is the suggestion to read [Ask] and Manual on how NOT to ask questions. This is not a service outsourcing site where you go through 3 problems in one question, and the people do it for you. Try to focus on the exact doubt, which facilitates.

  • 1

    I just didn’t edit your question because it would spoil the answers given, but that would be the real one, as you described: https://i.stack.Imgur.com/4QlNZ.png - if you always start from the real problem, it doesn’t take the time of colleagues, as done below, who attest to the interpretation as duplicate (that is, in fact the closure should be as "not clear", because came 2 elaborate answers that did not understand also)

  • 2

    If it is a list just recursively call the function, with while, for or like, or even with callbacks, since the case is javascript.

  • @Felipegoulart and another... we spent time answering your question you didn’t even make a comment even if it’s like "so it’s not that, guys". I just want to make that observation

Show 1 more comment

2 answers

1

You can use the api http://ip-api.com/docs/api:json

Example:

GET - http://ip-api.com/json/208.80.152.201

RETURN:

{
  "status": "success",
  "country": "United States",
  "countryCode": "US",
  "region": "CA",
  "regionName": "California",
  "city": "San Francisco",
  "zip": "94105",
  "lat": "37.7898",
  "lon": "-122.3942",
  "timezone": "America\/Los_Angeles",
  "isp": "Wikimedia Foundation",
  "org": "Wikimedia Foundation",
  "as": "AS14907 Wikimedia US network",
  "query": "208.80.152.201"
}

1

It’s quite simple, there are several websites that provide this type of service via API, one of them is the ipstack which I have found and is free (max 10,000 requests per month). The advantage of ipstack is that it provides various additional information such as country flag, IDD, continent code and others.

Just sign up and you will receive an API key, use is very simple:

http://api.ipstack.com/<ip>?access_key=<chave de api>

How I created an account I already have an API key (no problem putting it here)

http://api.ipstack.com/191.19.145.134?access_key=dd66d448ad3762f3480cbd96faddb276

The answer is:

{
    "ip":"191.19.145.134",
    "type":"ipv4",
    "continent_code":"SA",
    "continent_name":"South America",
    "country_code":"BR",
    "country_name":"Brazil",
    "region_code":"SP",
    "region_name":"Sao Paulo",
    "city":"S\u00e3o Paulo",
    "zip":"01323",
    "latitude":-23.5733,
    "longitude":-46.6417,
    "location":{
        "geoname_id":3448439,
        "capital":"Bras\u00edlia",
        "languages":[
            {
                "code":"pt",
                "name":"Portuguese",
                "native":"Portugu\u00eas"
            }
        ],
        "country_flag":"http:\/\/assets.ipstack.com\/flags\/br.svg",
        "country_flag_emoji":"\ud83c\udde7\ud83c\uddf7",
        "country_flag_emoji_unicode":"U+1F1E7 U+1F1F7",
        "calling_code":"55",
        "is_eu":false
    }
}

Here’s a practical example, remembering that it didn’t work on HTTPS requests (so you can’t test directly on Stackoverflow).

var api_key = "dd66d448ad3762f3480cbd96faddb276";

function LocalizaIP(ip) {
console.log("http://api.ipstack.com/"+ip+"?access_key="+api_key);
  $.ajax({
    method: "GET",
    url: "http://api.ipstack.com/"+ip+"?access_key="+api_key,   
  }).done(function (obj) {
    console.log(obj);
   });
};

LocalizaIP("191.19.145.134");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.