Get Country name from IP

Hello Everyone,

In this tutorial I am going to let you all know that how you can get country name from IP address.

I have used 2 apis for this task, Here are the links for the same:

To get location : http://ip-api.com/json

For Country Flag : https://restcountries.eu/rest/v2/name/{country_name}?fullText=true

So let’s start with the coding part I have used bootstrap and jquery to make this process easier

HTML code :

<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Get Country</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" />
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
</head>
<body class="bg-info">
<div class="container">
<div class="row">
<div class="col-md-6 p-5 bg-dark text-light mx-auto mt-5">
<table class="table">
<tbody>
<tr>
<td>Name</td>
<td id='country_name'></td>
</tr>
<tr>
<td>Flag</td>
<td id='country_flag'></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</body>
</html>

After this designing part I need to make some JS code to perform this:

JS Code :

<script>
var country = '';
function abcd(cc){
$.ajax({
url: 'https://restcountries.eu/rest/v2/name/'+cc+'?fullText=true',
success: function(data){
//country = alert(JSON.stringify(data[0].name));
$('#country_name').html(data[0].name);
$('#country_flag').html("<img src='"+data[0].flag+"' style='width:100%;'/>");
}
});
}
$(document).ready(function(){
$.ajax({
url: 'http://ip-api.com/json',
success: function(data){
country = data.country;
abcd(country);
}
});
});
</script>

So In this code I have used methods to ready and callback

When document is on ready stage, hit my first api to detect user’s location.

once I got user location from the api I have call new function as callback to get data of the same country.

Then shows data got from the API on the web page using jQuery .html().

Our output will looks as following image :

Get Country name from IP
Get Country name from IP

Here is the complete code for Get Country name from IP:

<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Get Country</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" />
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script>
var country = '';
function abcd(cc){
$.ajax({
url: 'https://restcountries.eu/rest/v2/name/'+cc+'?fullText=true',
success: function(data){
//country = alert(JSON.stringify(data[0].name));
$('#country_name').html(data[0].name);
$('#country_flag').html("<img src='"+data[0].flag+"' style='width:100%;'/>");
}
});
}
$(document).ready(function(){
$.ajax({
url: 'http://ip-api.com/json',
success: function(data){
country = data.country;
abcd(country);
}
});
});
</script>

</head>
<body class="bg-info">
<div class="container">
<div class="row">
<div class="col-md-6 p-5 bg-dark text-light mx-auto mt-5">
<table class="table">
<tbody>
<tr>
<td>Name</td>
<td id='country_name'></td>
</tr>
<tr>
<td>Flag</td>
<td id='country_flag'></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</body>
</html>

Hope you guys enjoy this tutorial.

26 thoughts on “Get Country name from IP

Leave a Reply

Your email address will not be published. Required fields are marked *

  1. aplikasi berita saham terkini Beautiful aspect of material. I simply stumbled upon your web site plus accession funds to convey that we acquire basically experienced bill ones website posts. In any manner I’ll be subscribing inside your supplement and even I actually results you receive admittance to constantly swiftly.

  2. Excellent read, I just passed this onto a colleague who was doing some research on that. And he actually bought me lunch since I found it for him smile Thus let me rephrase that: Thanks for lunch!

  3. I will right away grab your rss as I can’t in finding your e-mail subscription hyperlink or e-newsletter service. Do you have any? Kindly allow me understand so that I may just subscribe. Thanks.

  4. I loved as much as you’ll receive carried out right here. The sketch is tasteful, your authored material stylish. nonetheless, you command get got an impatience over that you wish be delivering the following. unwell unquestionably come more formerly again as exactly the same nearly very often inside case you shield this hike.

  5. Excellent read, I just passed this onto a colleague who was doing a little research on that. And he just bought me lunch since I found it for him smile So let me rephrase that: Thank you for lunch!

  6. Thanks for the sensible critique. Me and my neighbor were just preparing to do some research about this. We got a grab a book from our local library but I think I learned more from this post. I am very glad to see such magnificent info being shared freely out there.

  7. I was just seeking this information for a while. After 6 hours of continuous Googleing, at last I got it in your web site. I wonder what is the lack of Google strategy that don’t rank this kind of informative sites in top of the list. Generally the top web sites are full of garbage.

  8. I simply want to tell you that I am beginner to weblog and actually liked you’re web page. More than likely I’m want to bookmark your blog . You amazingly come with outstanding article content. Many thanks for revealing your web page.

  9. I precisely had to thank you very much once again. I do not know the things I could possibly have handled without the type of secrets revealed by you relating to that area of interest. Completely was a troublesome concern in my circumstances, nevertheless observing the very professional form you dealt with the issue forced me to leap for gladness. Extremely happy for your help as well as hope you recognize what a powerful job you are carrying out instructing the mediocre ones using your webblog. I am sure you’ve never encountered all of us.

  10. excellent publish, very informative. I ponder why the other specialists of this sector don’t understand this. You should proceed your writing. I am sure, you have a huge readers’ base already!

  11. Nice read, I just passed this onto a colleague who was doing some research on that. And he just bought me lunch since I found it for him smile Therefore let me rephrase that: Thanks for lunch!

  12. Oh my goodness! an incredible article dude. Thanks Nevertheless I am experiencing situation with ur rss . Don’t know why Unable to subscribe to it. Is there anyone getting equivalent rss downside? Anybody who is aware of kindly respond. Thnkx

  13. I wouldn’t have any idea generate income finished up below, however assumed this kind of publish appeared to be beneficial youtube terfavorit. I can’t acknowledge whom you are however definitely you will any well-known blog writer if you will not be presently. Kind regards!

Please wait...

Subscribe to our newsletter

Want to be notified when our article is published? Enter your email address and name below to be the first to know.