Node.js和PHP根據(jù)ip獲取地理位置的方法
更新時間:2014年03月14日 09:07:32 作者:
這篇文章主要介紹了Node.js和PHP根據(jù)ip獲取地理位置的方法,通過新浪接口根據(jù)IP地址獲取所在城市,需要的朋友可以參考下
一、Node.js實現(xiàn)代碼
var http = require('http');
var util = require('util');
/**
* 根據(jù) ip 獲取獲取地址信息
*/
var getIpInfo = function(ip, cb) {
var sina_server = 'http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=';
var url = sina_server + ip;
http.get(url, function(res) {
var code = res.statusCode;
if (code == 200) {
res.on('data', function(data) {
try {
cb(null, JSON.parse(data));
} catch (err) {
cb(err);
}
});
} else {
cb({ code: code });
}
}).on('error', function(e) { cb(e); });
};
getIpInfo('220.181.111.85', function(err, msg) {
console.log('城市: ' + msg.city);
console.log('msg: ' + util.inspect(msg, true, 8));
})
請求結(jié)果:
二、PHP實現(xiàn)代碼
請求結(jié)果:
復(fù)制代碼 代碼如下:
var http = require('http');
var util = require('util');
/**
* 根據(jù) ip 獲取獲取地址信息
*/
var getIpInfo = function(ip, cb) {
var sina_server = 'http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=';
var url = sina_server + ip;
http.get(url, function(res) {
var code = res.statusCode;
if (code == 200) {
res.on('data', function(data) {
try {
cb(null, JSON.parse(data));
} catch (err) {
cb(err);
}
});
} else {
cb({ code: code });
}
}).on('error', function(e) { cb(e); });
};
getIpInfo('220.181.111.85', function(err, msg) {
console.log('城市: ' + msg.city);
console.log('msg: ' + util.inspect(msg, true, 8));
})
請求結(jié)果:
復(fù)制代碼 代碼如下:
城市: 徐州
{
"ret": 1,
"start": "49.68.0.0",
"end": "49.68.255.255",
"country": "中國",
"province": "江蘇",
"city": "徐州",
"district": "",
"isp": "電信",
"type": "",
"desc": ""
}
{
"ret": 1,
"start": "49.68.0.0",
"end": "49.68.255.255",
"country": "中國",
"province": "江蘇",
"city": "徐州",
"district": "",
"isp": "電信",
"type": "",
"desc": ""
}
二、PHP實現(xiàn)代碼
復(fù)制代碼 代碼如下:
<?
$ip = "220.181.111.85";
$url = "http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=$ip";
$data = file_get_contents($url);
$result = json_decode($data);
echo "城市:" . $result->city . "<br>";
print_r($result);
?>
$ip = "220.181.111.85";
$url = "http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=$ip";
$data = file_get_contents($url);
$result = json_decode($data);
echo "城市:" . $result->city . "<br>";
print_r($result);
?>
請求結(jié)果:
復(fù)制代碼 代碼如下:
城市:徐州
stdClass Object
(
[ret] => 1
[start] => 49.68.0.0
[end] => 49.68.255.255
[country] => 中國
[province] => 江蘇
[city] => 徐州
[district] =>
[isp] => 電信
[type] =>
[desc] =>
)
stdClass Object
(
[ret] => 1
[start] => 49.68.0.0
[end] => 49.68.255.255
[country] => 中國
[province] => 江蘇
[city] => 徐州
[district] =>
[isp] => 電信
[type] =>
[desc] =>
)
相關(guān)文章
javascript實現(xiàn)的猜數(shù)小游戲完整實例代碼
這篇文章主要介紹了javascript實現(xiàn)的猜數(shù)小游戲,游戲中用戶共有10次猜測機會,并且每次都有不同的提示信息,該游戲涉及javascript流程控制與數(shù)值運算的相關(guān)技巧,需要的朋友可以參考下2016-05-05
JavaScript函數(shù)及其prototype詳解
這篇文章主要介紹了JavaScript函數(shù)及其prototype詳解的相關(guān)資料,需要的朋友可以參考下2023-03-03
javascript仿163網(wǎng)盤無刷新文件上傳系統(tǒng)
這個仿163網(wǎng)盤無刷新文件上傳系統(tǒng),并沒有用使用.net的控件,完全的手工制作。2008-10-10

