如何从php中的用户IP地址获取用户国家/地区名称

时间:2016-05-09 09:10:28

标签: php

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

5 个答案:

答案 0 :(得分:8)

试试这个

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; 

答案 1 :(得分:2)

使用apinotes外部geolocation api

示例:

http://apinotes.com/ipaddress/ip.php?ip=27.62.184.235

URL:  http://apinotes.com/ipaddress/ip.php
Parameter Name: ip
Value:  27.62.184.23 (ipv4 address)

<强>输出: -

这里我们以Json查询格式显示IP地址详细信息。

{ 
   "status":"success",
   "ip":"27.62.184.23",
   "country_name":"India",
   "country_code":"IN",
   "country_code3":"IND",
   "region_code":"16",
   "region_name":"Maharashtra",
   "city_name":"Mumbai",
   "latitude":18.975,
   "longitude":72.8258
}

答案 2 :(得分:1)

要获取IP地址,您可以使用您提供的功能,或者只需使用

  

$ _ SERVER [&#39; REMOTE_ADDR&#39;]

获取用户的IP地址,如下所示:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

if (cell== nil) {

    cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
}
if(_List.count>0) {

   //Use to show data

   cell.textLabel.text = [NSString stringWithFormate:@"%@",[_List objectAtIndex:indexPath.row]]];

   // [cell setupCell:[_List objectAtIndex:indexPath.row]];
   // ViewControllerB *vc=[[ViewController alloc]init];
   // vc.userIdInfo=[_List objectAtIndex:indexPath.row];
}
return cell;

答案 3 :(得分:0)

您可以执行此操作,其中包括货币:

<?
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";
?>

答案 4 :(得分:0)

获取当前国家/地区名称和国家/地区lat long值:

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