admin 发表于 2023-11-22 18:23:30

PHP获取IP地址 包含国家省份城市

<?php
header("Content-type: text/html; charset=utf-8");

//取IP信息
function getIpAddress($ip=""){
        $url = "http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js";
        if($ip!="")$url .= "&ip=".$ip;
    $ipContent   = file_get_contents($url);
    $jsonData = explode("=",$ipContent);   
    $jsonAddress = substr($jsonData, 0, -1);
    return $jsonAddress;
}
//返回IP的地理地址
function showIpAddress($ip=""){
        if($ip){
                $cityArray = getIpAddress($ip);
                $city_json = json_decode($cityArray,TRUE);
                $address = $city_json['country'].''.$city_json['province'].''.$city_json['city'];
                return $address;
        }else{
                return '';
        }
}

$ip = '119.134.90.110';
//echo getIpAddress($ip);
//echo '<br>';
echo showIpAddress($ip);
echo '<br>';
?>
页: [1]
查看完整版本: PHP获取IP地址 包含国家省份城市