<?php
//根据IP地址获取其地理位置(国家,省份,城市等)的方法
function GetIpLookup($ip = ''){  
  if(empty($ip)){  
    return '请输入IP地址'; 
  }  
  $res = @file_get_contents('http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip=' . $ip);  
  if(empty($res)){
    return false;
  }  
  $jsonMatches = array();  
  preg_match('#\{.+?\}#', $res, $jsonMatches);  
  if(!isset($jsonMatches[0])){
    return false;
  }  
  $json = json_decode($jsonMatches[0], true);  
  if(isset($json['ret']) && $json['ret'] == 1){  
    $json['ip'] = $ip;  
    unset($json['ret']);  
  }else{  
    return false;  
  }  
  return $json;  
} 

$ipInfos = GetIpLookup('192.30.253.112'); //测试 github.com 的IP地址

echo '<pre>';
print_r($ipInfos); 
echo '</pre>';

 

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注