A programming & designing blog!

LightBlog
Responsive Ads Here

Sunday, November 3, 2013

Get IP Geolocation with PHP

There is no build in way or tool in PHP to get geographical location of an IP address. Thats why, we will need to use a third party api to get geographical location of an IP address.

There are many free and paid Geo apis are available today to get geolocation from an IP address. In this post I will use freegeoip.net api. It is very simple and easy. We do not need to register for api key and the most impotent thing is this api is absolutely free!

The ip2location Function

function ip2location($ip)
{
    # api url
    $api = 'http://freegeoip.net/json/' . $ip;

    # connect api with curl
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $api);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
    $data = curl_exec($ch);
    curl_close($ch);

    # return reposed data
    return json_decode($data);
}

Usages

$geo = ip2location('8.8.8.8');
$geo->ip;
$geo->country_name;
$geo->region_name;
$geo->city;
$geo->zipcode;
$geo->areacode;

24 comments:

  1. Thanks for your script! It works great!

    ReplyDelete
  2. Great script, thanks!

    Just a small tip: you should declare your fonction as the following:
    function ip2location($ip = '')
    So that if we don't provide an IP, it will automatically use the client's IP.

    ReplyDelete
  3. You can also use :
    $geo->metro_code;
    $geo->latitude;
    $geo->longitude;

    ReplyDelete
  4. Thanks for your information. I have checked ip address location from this site WhoisXY.com . Here i got ip address location,country,etc..

    ReplyDelete
  5. That was actually really simple i'm used to working with the maxmind.com one this method & api is so much more lightweight.
    thanks for sharing the information

    ReplyDelete
  6. Hi, do you have a real example that could allow to copy past ? I know nothing but I would like to work on it :)
    Many thanks for your help.

    ReplyDelete
  7. Nguyên liệu bằng silicon, mềm mại , nhẹ nhàng và êm tay khi cầm máy,có thể tháo gỡ dễ dàng và nhanh chóng. Đảm bảo hàng mới 100%
    [URL=http://lambaocaotaichinh.com/lop-hoc-ke-toan-tren-phan-mem-fast-misa.html]học kế toán phần mềm misa fast[/URL]
    [URL=http://lambaocaotaichinh.com/lop-hoc-ke-toan-tren-phan-mem-excel.html]học kế toán phần mềm excel[/URL]
    [URL=http://lambaocaotaichinh.com/lop-hoc-ke-toan-tong-hop-tai-ninh-binh.html]trung tâm kế toán tại ninh bình[/URL]
    Review: Yeezy Moon Rock & Adidas NMD
    Hai Sản phẩm HOT nhất của Adidas tại thời điểm hiện tại

    ReplyDelete
  8. Great and useful article. Creating content regularly is very tough. Your points are motivated me to move on.


    SEO Company in Chennai

    ReplyDelete
  9. Shorouk Shorouk for all household services from Cleaning of apartments, cleaning apartments, insect control and sewerage, offering you all the domestic services at the cheapest prices. Contact us to request the service you want Dear customer
    شركة تنظيف بخميس مشيط
    شركة مكافحة حشرات بخميس مشيط
    شركة تنظيف مجالس بخميس مشيط

    شركة تنظيف خزانات بالاحساء
    شركة تنظيف خزانات بابها
    افضل شركة تنظيف خزانات بالاحساء


    ReplyDelete
  10. I have had a lot of harvest after watching this article from you! I feel it interesting, your post gave me a new perspective! I have read many other articles about the same topic, but your article convinced me! I hope you continue to have high quality articles like this to share with veryone!
    vex 3

    ReplyDelete
  11. It is the information I am looking for. I'm learning more about this. Hope you bring more things related to it. Thank you very much!
    abcya

    ReplyDelete
  12. This article is very nice and helpful, I really like this article. We also provide run 3

    ReplyDelete
  13. a pride for me to be able to discuss on a quality website because I just learned to make an article on
    cara menggugurkan kandungan

    ReplyDelete
  14. I have read many other articles about the same topic, but your article convinced me! I hope you continue to have high quality articles like this to share with veryone!
    happy wheels

    ReplyDelete
  15. I have read through other blogs, but they are cumbersome and confusing more than your post. I hope you continue to have such quality articles to share with everyone! I believe a lot of people will be surprised to read this article!
    shell shockers

    ReplyDelete
  16. Thank you for this post!! I have just discovered your blog recently and I really like it! I will definitely try some of your insights.
    Best selenium training in chennai
    selenium Classes in chennai

    ReplyDelete