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; }
|