一、Node.js實作程式碼
var http = require( 'http');
var util = require('util');
/**
* 依 ip 取得位址資訊
*/
var getIpInfo = function(ip, cb) {
= '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) {
try {
地JSON.parse(data));
} catch (err) {
}
});
} });
}
}).on('error', function(e) { cb(e); });
};
getIpInfo,21185. , function(err, msg) {
console.log('城市: ' msg.city);
console.log('msg: ' util.inspect(msg, true, 8));
})
請求結果:
程式碼如下:
城市🎜>{
"ret": 1,
"start": "49.68.0.0",
"end": "49.68.255.255", "province": "江蘇",
"city": "徐州",
"district": "",
"isp": "district": "",
",
"desc": ""
}
二、PHP實現代碼
$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 . "
";
print_r($result);
?>
請求結果:
stdClass Object
(
[ret] => 1
(
[ret] => 1
🎜> 0.0
[end] => 49.68.255.255
[country] => 中
[province] => 江蘇
🎜> [province] => 江蘇
🎜> [province] => 江蘇
[isp] => 電訊
[type] =>
[desc] =>
)