PHP uses Sina IP interface to implement IP address location specific city ____php

Source: Internet
Author: User

Http://www.smohan.net/blog/14.html

Before the station using Taobao interface to achieve IP and geographical information conversion, seemingly recently Taobao IP server error, resulting in file_get_contents can not get content and JSON return, so, for a more stable Sina IP address library to achieve some of the functions of this site. Sina provides open IP address library data for developers to call, address: http://int.dpool.sina.com.cn/iplookup/iplookup.php, you can return the current IP in the provinces and cities of information. Specific Demo demo Please refer to the site message

First: Let's use a frequently seen function getip () to achieve IP acquisition:

function GetIP () {if (getenv ("Http_client_ip") && strcasecmp (getenv ("Http_client_ip"), "Unknown") {$ip = Getenv ("Http_client_ip"); else if (getenv ("Http_x_forwarded_for") && strcasecmp (getenv ("Http_x_forwarded_for"), "Unknown") {$ip = Getenv ("Http_x_forwarded_for"); else if (getenv ("REMOTE_ADDR") && strcasecmp (getenv ("REMOTE_ADDR"), "Unknown")) {$ip = getenv ("REMOTE_ADDR"); else if (isset ($_server[' remote_addr ']) && $_server[' remote_addr '] && strcasecmp ($_server[' Remote_ ADDR '], "unknown")) {$ip = $_server[' remote_addr '];} else {$ip = "Unknown";} return $IP; }
Close Code

The second step: the IP address will be obtained by passing a Sina IP interface, using a commonly used function file_get_contents to obtain its JSON format return value, so that we can achieve the IP address and the corresponding provincial city conversion;

function getLocation ($ip = ') {empty ($IP) && $ip = GetIP (); if ($ip = = "127.0.0.1") return "native address"

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.