日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

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

發(fā)布時間:2024/3/24 php 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php ip获取邮政编码,地理位置-如何从IP地址确定邮政编码和城市? 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

以下是我發(fā)現(xiàn)的使用[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" }

總結(jié)

以上是生活随笔為你收集整理的php ip获取邮政编码,地理位置-如何从IP地址确定邮政编码和城市?的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。