ASp.NET 如何根据IP查询所在城市
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ASp.NET 如何根据IP查询所在城市相关的知识,希望对你有一定的参考价值。
我想做一个功能: 用户打开 网站 能根据 他的IP 查询出 所在城市
哪里可以找到查询的数据库? 或者有什么方法?
这个IP转换的函数用的很多,他的功能就是把IP地址转换成十进制的数值,用来方便数据库查询的。
因为如果IP是字符型,那是不好查询的,所以就做了个转换,这样你就可以用这种方式查询了:
select count(*) from LockIP where ip1 <="&num&" and ip2 >="&num&"
但这个函数并不是直接用来查询IP对应城市的,它应该是用来查找某个IP是否在被锁定的IP段范围内的,不过你可以稍加修改,它就可以用来查找城市了,因为这个IP表里应该有城市字段的,
这样改:
sql = "select city from [你的IP表] where ip1 <=" & num & " and ip2 >=" & num
dim city=""
set rs=conn.execute(sql)
if rs <> null and rs.count > 0 then
city = rs(0)
end if
其中的city对应你表中的城市字段
追问
我不是说怎么从数据库查找, 我是说我没有数据库 ,我去哪里找数据库?
参考技术A 这个我也很想知道,IP一般都是分段安排的,每个城市都有自己的IP段,所以说这些东西应该是死的,就是不知道这些东西是怎么规定的,网上查一查是否可以找到 参考技术B /// <summary>/// 从http://www.ip138.com/读取IP地址
/// </summary>
/// <returns></returns>
public static string GetIpAddress(string Ip)
WebClient obj = new WebClient();
string strR = "";
try
strR = Regex.Replace(obj.DownloadString(string.Format("http://www.ip138.com/ips.asp?ip=0&action=2",Ip)), "\r\n", "", RegexOptions.IgnoreCase);
catch return "未知";
return Regex.Replace(GetValueAnd("</li><li>参考数据一:", "</li></ul>", strR), "</li><li>", "|", RegexOptions.IgnoreCase);
/// <summary>
/// 获取两个字符串之间的字符
/// </summary>
/// <returns></returns>
public static string GetValueAnd(string strStart, string strEnd, string text)
if (string.IsNullOrEmpty(text))
return "";
string regex = @"^.*" + strStart + "(?<content>.+?)" + strEnd + ".*$";
Regex rgClass = new Regex(regex, RegexOptions.Singleline | RegexOptions.IgnoreCase);
Match match = rgClass.Match(text);
return match.Groups["content"].Value;
根据ip判断返回城市名称查询当地天气
<?php
header("content-type:text/html;charset=utf-8");
date_default_timezone_set("Asia/Shanghai");
error_reporting(0);
// 根据IP判断城市
$user_ip = $_SERVER[‘REMOTE_ADDR‘];
$url ="http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=$user_ip";
$address = file_get_contents($url);
$address_arr = json_decode($address); //返回对象,需要转换为数组
//以上海为例
stdClass Object | |
( | |
[ret] => 1 | |
[start] => -1 | |
[end] => -1 | |
[country] => 中国 | |
[province] => 上海 | |
[city] => 上海 | |
[district] => | |
[isp] => | |
[type] => | |
[desc] => | |
) |
function object_array($array){
if(is_object($array)){
$array = (array)$array;
}
if(is_array($array)){
foreach($array as $key=>$value){
$array[$key] = object_array($value);
}
}
return $array;
}
//通过该函数转化为数组
$address_arr = object_array($address_arr);
// print_r($address_arr);
Array | |
( | |
[ret] => 1 | |
[start] => -1 | |
[end] => -1 | |
[country] => 中国 | |
[province] => 上海 | |
[city] => 上海 | |
[district] => | |
[isp] => | |
[type] => | |
[desc] => | |
) |
$city = $address_arr["city"];
//输出为上海,获得城市通过百度天气API查询当地天气
$con=file_get_contents("http://api.map.baidu.com/telematics/v3/weather?location=$city&output=json&ak=spmMww7Eoqcmf3FXbnLyDUwL"); //注意ak值需要进入百度接口注册,附上地址:http://lbsyun.baidu.com/apiconsole/key
$con = json_decode($con);
print_r($con);
stdClass Object | |
( | |
[error] => 0 | |
[status] => success | |
[date] => 2016-09-23 | |
[results] => Array | |
( | |
[0] => stdClass Object | |
( | |
[currentCity] => 上海 | |
[pm25] => 42 | |
[index] => Array | |
( | |
[0] => stdClass Object | |
( | |
[title] => 穿衣 | |
[zs] => 热 | |
[tipt] => 穿衣指数 | |
[des] => 天气热,建议着短裙、短裤、短薄外套、T恤等夏季服装。 | |
) | |
[1] => stdClass Object | |
( | |
[title] => 洗车 | |
[zs] => 较适宜 | |
[tipt] => 洗车指数 | |
[des] => 较适宜洗车,未来一天无雨,风力较小,擦洗一新的汽车至少能保持一天。 | |
) | |
[2] => stdClass Object | |
( | |
[title] => 旅游 | |
[zs] => 适宜 | |
[tipt] => 旅游指数 | |
[des] => 天气较好,但丝毫不会影响您出行的心情。温度适宜又有微风相伴,适宜旅游。 | |
) | |
[3] => stdClass Object | |
( | |
[title] => 感冒 | |
[zs] => 少发 | |
[tipt] => 感冒指数 | |
[des] => 各项气象条件适宜,无明显降温过程,发生感冒机率较低。 | |
) | |
[4] => stdClass Object | |
( | |
[title] => 运动 | |
[zs] => 较适宜 | |
[tipt] => 运动指数 | |
[des] => 天气较好,户外运动请注意防晒。推荐您进行室内运动。 | |
) | |
[5] => stdClass Object | |
( | |
[title] => 紫外线强度 | |
[zs] => 弱 | |
[tipt] => 紫外线强度指数 | |
[des] => 紫外线强度较弱,建议出门前涂擦SPF在12-15之间、PA+的防晒护肤品。 | |
) | |
) | |
[weather_data] => Array | |
( | |
[0] => stdClass Object | |
( | |
[date] => 周五 09月23日 (实时:26℃) | |
[dayPictureUrl] => http://api.map.baidu.com/images/weather/day/duoyun.png | |
[nightPictureUrl] => http://api.map.baidu.com/images/weather/night/duoyun.png | |
[weather] => 多云 | |
[wind] => 东风微风 | |
[temperature] => 27 ~ 21℃ | |
) | |
[1] => stdClass Object | |
( | |
[date] => 周六 | |
[dayPictureUrl] => http://api.map.baidu.com/images/weather/day/duoyun.png | |
[nightPictureUrl] => http://api.map.baidu.com/images/weather/night/duoyun.png | |
[weather] => 多云 | |
[wind] => 东风微风 | |
[temperature] => 28 ~ 23℃ | |
) | |
[2] => stdClass Object | |
( | |
[date] => 周日 | |
[dayPictureUrl] => http://api.map.baidu.com/images/weather/day/duoyun.png | |
[nightPictureUrl] => http://api.map.baidu.com/images/weather/night/yin.png | |
[weather] => 多云转阴 | |
[wind] => 东风微风 | |
[temperature] => 28 ~ 23℃ | |
) | |
[3] => stdClass Object | |
( | |
[date] => 周一 | |
[dayPictureUrl] => http://api.map.baidu.com/images/weather/day/duoyun.png | |
[nightPictureUrl] => http://api.map.baidu.com/images/weather/night/yin.png | |
[weather] => 多云转阴 | |
[wind] => 东北风微风 | |
[temperature] => 29 ~ 25℃ | |
) | |
) | |
) | |
) | |
) |
$arr = object_array($con); //继续转成数组操作
$detail = $arr["results"][0]["weather_data"];
$city = $arr["results"][0]["currentCity"];
print_r($detail);
Array | |
( | |
[0] => Array | |
( | |
[date] => 周五 09月23日 (实时:26℃) | |
[dayPictureUrl] => http://api.map.baidu.com/images/weather/day/duoyun.png | |
[nightPictureUrl] => http://api.map.baidu.com/images/weather/night/duoyun.png | |
[weather] => 多云 | |
[wind] => 东风微风 | |
[temperature] => 27 ~ 21℃ | |
) | |
[1] => Array | |
( | |
[date] => 周六 | |
[dayPictureUrl] => http://api.map.baidu.com/images/weather/day/duoyun.png | |
[nightPictureUrl] => http://api.map.baidu.com/images/weather/night/duoyun.png | |
[weather] => 多云 | |
[wind] => 东风微风 | |
[temperature] => 28 ~ 23℃ | |
) | |
[2] => Array | |
( | |
[date] => 周日 | |
[dayPictureUrl] => http://api.map.baidu.com/images/weather/day/duoyun.png | |
[nightPictureUrl] => http://api.map.baidu.com/images/weather/night/yin.png | |
[weather] => 多云转阴 | |
[wind] => 东风微风 | |
[temperature] => 28 ~ 23℃ | |
) | |
[3] => Array | |
( | |
[date] => 周一 | |
[dayPictureUrl] => http://api.map.baidu.com/images/weather/day/duoyun.png | |
[nightPictureUrl] => http://api.map.baidu.com/images/weather/night/yin.png | |
[weather] => 多云转阴 | |
[wind] => 东北风微风 | |
[temperature] => 29 ~ 25℃ | |
) | |
) |
//获得天气数据,根据自己需求进行调整
?>
以上是关于ASp.NET 如何根据IP查询所在城市的主要内容,如果未能解决你的问题,请参考以下文章