【问题标题】:how to get user country name from user ip address in php如何从php中的用户IP地址获取用户国家名称
【发布时间】:2016-09-03 20:28:47
【问题描述】:

如何从用户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;
}

【问题讨论】:

标签: php


【解决方案1】:

获取当前国家名称和国家纬度长值:

$IPaddress = !empty($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'];
$details = $this->ip_details("$IPaddress");
$country_name = $details->country_name;

$geocode_stats = file_get_contents("http://maps.googleapis.com/maps/api/geocode/json?address=$country_name&sensor=false");
$output_deals = json_decode($geocode_stats);
$latLng = $output_deals->results[0]->geometry->location;
echo $lat = $latLng->lat;
echo $lng = $latLng->lng;

【讨论】:

    【解决方案2】:

    您可以这样做,其中包括货币:

    <?
    function resolveIP($ip) {
      $string = file_get_contents("https://ipsidekick.com/{$ip}");
      $json = json_decode($string);
      return $json;
    }
    
    $ip = '17.142.160.59';
    $json = resolveIP($ip);
    
    echo "Country name: {$json->country->name}\n";
    echo "Currency name: {$json->currency->name}\n";
    echo "Public holiday: {$json->currency->holiday}\n";
    ?>
    

    【讨论】:

      【解决方案3】:

      要获取IP地址,您可以使用您提供的功能,也可以直接使用

      $_SERVER['REMOTE_ADDR']

      获取用户的IP地址使用如下:

      $ip =  $_SERVER['REMOTE_ADDR'];   // ip = 8.8.8.8
      $country = file_get_contents('http://ipinfo.io/8.8.8.8/country'); // for more info visit [enter link description here][1] 
      echo $country;  // output = US | UK | IN as per ip (just one country code)
      

      【讨论】:

        【解决方案4】:

        试试这个

        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; 
        

        【讨论】:

        • 如果我想得到国家的货币是怎么回事?
        • @GhulamAbbas 您可以尝试在结果中包含货币的 usercountry.com。 $result = json_decode(file_get_contents("usercountry.com/v1.0/json{$IPaddress}"), true);然后你可以做 $result['currency']['code'].
        猜你喜欢
        • 1970-01-01
        • 2013-11-05
        • 2012-03-08
        • 1970-01-01
        • 1970-01-01
        • 2016-02-04
        • 1970-01-01
        • 2011-04-08
        • 2017-01-28
        相关资源
        最近更新 更多