Home  >  Article  >  Backend Development  >  PHP Taobao IP data to obtain user IP and geographical location_PHP tutorial

PHP Taobao IP data to obtain user IP and geographical location_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 16:57:02829browse

Sometimes we don’t want to use our own database to store IP addresses. Our own IP library is not updated in time. We can directly use a third-party IP library to operate. Here is an introduction to how to use Taobao IP data to obtain user IP and geographical location.

Open the following address directly in IE browser

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

Return information

The code is as follows
 代码如下 复制代码

{"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"}}

Copy code

 代码如下 复制代码

/**
 * 获取 IP  地理位置
 * 淘宝IP接口
 * @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;
}

{"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"}}


We entered it directly in the IE address above. Here we use the php file_get_contents function to obtain
 代码如下 复制代码


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

The code is as follows Copy code
/** * Get IP geographical location * 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 through file_get_contents is converted into an array using the php json_decode function. We need to provide the $IP address. Here is a function to obtain the user’s real IP address
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; }

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631546.htmlTechArticleSometimes we don’t want to use our own database to store IP addresses. Our IP database is not updated in a timely manner. We can use it directly. Use a third-party IP library to operate. Here is an introduction to using Taobao IP data to obtain...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn