PHP Taobao IP data acquisition user IP address and geographic location

Source: Internet
Author: User

Open the following address in IE browser

Http://ip.taobao.com/service/getIpInfo.php? Ip = 8.8.8.8

Response Information

The code is as follows: Copy code

{"Code": 0, "data": {"country": "u7f8eu56fd", "country_id": "US", "area": "", "area_id ": "", "region": "", "region_id": "", "city": "", "city_id": "", "county ":"", "county_id": "", "isp": "", "isp_id": "", "ip": "8.8.8.8 "}}

In the above example, we enter the ie address directly. Here we use the php file_get_contents function to obtain

The code is as follows: Copy code

/**
* Obtain the geographic location of an IP address
* Taobao IP interface
* @ Return: array
*/
Function getCity ($ ip)
{
$ Url = "http://ip.taobao.com/service/getIpInfo.php? Ip = ". $ ip;
$ Ip = json_decode (file_get_contents ($ url ));
If (string) $ ip-> code = '1 '){
Return false;
  }
$ Data = (array) $ ip-> data;
Return $ data;
}


The above reason is that the json format data returned by Taobao via file_get_contents is converted to an array using the php json_decode function.

$ The IP address must be provided. The following provides a function to obtain the real IP address of a user.

The code is as follows: Copy code


Function getIP ()
{
Static $ realip;
If (isset ($ _ SERVER )){
If (isset ($ _ SERVER ["HTTP_X_FORWARDED_FOR"]) {
$ Realip = $ _ SERVER ["HTTP_X_FORWARDED_FOR"];
} Else if (isset ($ _ SERVER ["HTTP_CLIENT_IP"]) {
$ Realip = $ _ SERVER ["HTTP_CLIENT_IP"];
} Else {
$ Realip = $ _ SERVER ["REMOTE_ADDR"];
        }
} Else {
If (getenv ("HTTP_X_FORWARDED_FOR ")){
$ Realip = getenv ("HTTP_X_FORWARDED_FOR ");
} Else if (getenv ("HTTP_CLIENT_IP ")){
$ Realip = getenv ("HTTP_CLIENT_IP ");
} Else {
$ Realip = getenv ("REMOTE_ADDR ");
        }
    }
 
 
Return $ realip;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.