如何从用户IP地址获取国家名称

我有用户的IP地址,我根据用户的IP地址获得了用户的国家/地区名称
php中怎么可能?

function get_client_ip() {
    $ipaddress = '';
    if (getenv('HTTP_CLIENT_IP'))
        $ipaddress = getenv('HTTP_CLIENT_IP');
    else if(getenv('HTTP_X_FORWARDED_FOR'))
        $ipaddress = getenv('HTTP_X_FORWARDED_FOR');
    else if(getenv('HTTP_X_FORWARDED'))
        $ipaddress = getenv('HTTP_X_FORWARDED');
    else if(getenv('HTTP_FORWARDED_FOR'))
        $ipaddress = getenv('HTTP_FORWARDED_FOR');
    else if(getenv('HTTP_FORWARDED'))
       $ipaddress = getenv('HTTP_FORWARDED');
    else if(getenv('REMOTE_ADDR'))
        $ipaddress = getenv('REMOTE_ADDR');
    else
        $ipaddress = 'UNKNOWN';
    return $ipaddress;
}

最佳答案

试试这个

function ip_details($IPaddress)
{
    $json       = file_get_contents("http://ipinfo.io/{$IPaddress}");
    $details    = json_decode($json);
    return $details;
}

$IPaddress  =  'Your ip address of user';

$details    =   ip_details("$IPaddress");

//echo $details->city;
 echo $details->country;
//echo $details->org;
//echo $details->hostname;

关于php - 如何从PHP中的用户IP地址获取用户国家/地区名称,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37111788/

10-12 12:56