php ip获取邮政编码,地理位置-如何从IP地址确定邮政编码和城市?

以下是我发现的使用[http://ipinfodb.com/ip_locator.php]来获取其信息的代码段的修改版本。 请记住,您也可以向他们申请API密钥,并直接使用API来获取您认为合适的信息。 如您所见[http://ipinfodb.com/ip_location_api.php,]他们提供了从PHP到JavaScript到ASP.Net的所有示例。 如前所述,下面不需要密钥,因为它可以拉动其公共页面,而Regex可以通过该页面获取指定的信息。 钥匙是免费的。

片段

function detect_location($ip=NULL, $asArray=FALSE) {

if (empty($ip)) {

if (!empty($_SERVER['HTTP_CLIENT_IP'])) { $ip = $_SERVER['HTTP_CLIENT_IP']; }

elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { $ip = $_SERVER['HTTP_X_FORWARDED_FOR']; }

else { $ip = $_SERVER['REMOTE_ADDR']; }

}

elseif (!is_string($ip) || strlen($ip) < 1 || $ip == '127.0.0.1' || $ip == 'localhost') {

$ip = '8.8.8.8';

}

$url = 'http://ipinfodb.com/ip_locator.php?ip=' . urlencode($ip);

$i = 0; $content; $curl_info;

while (empty($content) && $i < 5) {

$ch = curl_init();

$curl_opt = array(

CURLOPT_FOLLOWLOCATION => 1,

CURLOPT_HEADER => 0,

CURLOPT_RETURNTRANSFER => 1,

CURLOPT_URL => $url,

CURLOPT_TIMEOUT => 1,

CURLOPT_REFERER => 'http://' . $_SERVER['HTTP_HOST'],

);

if (isset($_SERVER['HTTP_USER_AGENT'])) $curl_opt[CURLOPT_USERAGENT] = $_SERVER['HTTP_USER_AGENT'];

curl_setopt_array($ch, $curl_opt);

$content = curl_exec($ch);

if (!is_null($curl_info)) $curl_info = curl_getinfo($ch);

curl_close($ch);

}

$araResp = array();

if (preg_match('{

City : ([^}i', $content, $regs)) $araResp['city'] = trim($regs[1]);

if (preg_match('{

State/Province : ([^}i', $content, $regs)) $araResp['state'] = trim($regs[1]);

if (preg_match('{

Country : ([^

if (preg_match('{

Zip or postal code : ([^}i', $content, $regs)) $araResp['zip'] = trim($regs[1]);

if (preg_match('{

Latitude : ([^}i', $content, $regs)) $araResp['latitude'] = trim($regs[1]);

if (preg_match('{

Longitude : ([^}i', $content, $regs)) $araResp['longitude'] = trim($regs[1]);

if (preg_match('{

Timezone : ([^}i', $content, $regs)) $araResp['timezone'] = trim($regs[1]);

if (preg_match('{

Hostname : ([^}i', $content, $regs)) $araResp['hostname'] = trim($regs[1]);

$strResp = ($araResp['city'] != '' && $araResp['state'] != '') ? ($araResp['city'] . ', ' . $araResp['state']) : 'UNKNOWN';

return $asArray ? $araResp : $strResp;

}

使用

detect_location();

// returns "CITY, STATE" based on user IP

detect_location('xxx.xxx.xxx.xxx');

// returns "CITY, STATE" based on IP you provide

detect_location(NULL, TRUE); // based on user IP

// returns array(8) { ["city"] => "CITY", ["state"] => "STATE", ["country"] => "US", ["zip"] => "xxxxx", ["latitude"] => "xx.xxxxxx", ["longitude"] => "-xx.xxxxxx", ["timezone"] => "-07:00", ["hostname"] => "xx-xx-xx-xx.host.name.net" }

detect_location('xxx.xxx.xxx.xxx', TRUE); // based on IP you provide

// returns array(8) { ["city"] => "CITY", ["state"] => "STATE", ["country"] => "US", ["zip"] => "xxxxx", ["latitude"] => "xx.xxxxxx", ["longitude"] => "-xx.xxxxxx", ["timezone"] => "-07:00", ["hostname"] => "xx-xx-xx-xx.host.name.net" }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值