实现根据IP显示当地天气的功能,现在测试各个平台给予的IP库的准确度问题;
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>不同接口天气预报</title>
</head>
<body>
<?php
//得到用户真实的IP地址
$ip = getRealIP();
//使用接口得到返回的信息
echo "<br />腾讯IP接口测试:<br />";
$qq_url="http://ip.qq.com/cgi-bin/searchip?searchip1=".$ip;
$json_qq = file_get_contents($qq_url);
$json_qq = iconv('gbk','utf-8//IGNORE',$json_qq);
preg_match("@<span>(.*)</span></p>@iU",$json_qq,$arr_qq);
var_dump($arr_qq[1]);
echo "<br />淘宝IP接口测试:<br />";
$taobao_url="http://ip.taobao.com/service/getIpInfo.php?ip=".$ip;
$json_taobao = file_get_contents($taobao_url);
$arr_taobao=json_decode($json_taobao);
if((string)$arr_taobao->code=='1'){
return false;
}
$arr_taobao = (array)$arr_taobao->data;
$arr_ipdata = $arr_taobao;
var_dump($arr_taobao);
echo "<br />新浪IP接口测试:<br />";
$sina_url = "http://counter.sina.com.cn/ip?ip=".$ip;
$json_sina = file_get_contents($sina_url);
$json_sina = iconv('gbk','utf-8//IGNORE',$json_sina);
echo $json_sina;
echo "<br />搜狐IP接口测试:<br />";
$sohu_url = "http://pv.sohu.com/cityjson?ie=utf-8";
$json_sohu = file_get_contents($sohu_url);
echo $json_sohu;
echo "<br />网易有道IP接口测试:<br />";
$youdao_url = "http://www.youdao.com/smartresult-xml/search.s?type=ip&q=".$ip;
$json_youdao = file_get_contents($youdao_url);
$json_youdao = iconv('gbk','utf-8//IGNORE',$json_youdao);
echo $json_youdao;
//提取最精确城市
if(!empty($arr_ipdata['county'])){
$region_cur = $arr_ipdata['county'];
$regionid_cur = $arr_ipdata['county_id'];
}elseif(!empty($arr_ipdata['city_id'])){
$region_cur = $arr_ipdata['city'];
$regionid_cur = $arr_ipdata['city_id'];
}elseif(!empty($arr_ipdata['region_id'])){
$region_cur = $arr_ipdata['region'];
$regionid_cur = $arr_ipdata['region_id'];
}elseif(!empty($arr_ipdata['area_id'])){
$region_cur = $arr_ipdata['area'];
$regionid_cur = $arr_ipdata['area_id'];
}else{
$region_cur = '';
}
echo '<br /> 当前城市: '.$region_cur.$regionid_cur.'<br />';
//根据城市找到对应的天气网城市代码;
$city_cur = str_replace('市','',$region_cur);
$xml_arr = simplexml_load_file("city_weather_code.xml");
$key_arr = (array)$xml_arr->dict->key;
print_r($key->dict->key);
foreach($key_arr as $k=>$v){
if($v == $city_cur){
$num = $k;
break;
}
}
$string_arr = (array)$xml_arr->dict->string;
$code = $string_arr[$num];
echo $code;
//获取当地的的天气
$url_weather = "http://m.weather.com.cn/data/".$code.".html";
$json_weather = file_get_contents($url_weather);
//mb_convert_encoding($json_weather,'utf-8');
echo $json_weather;
/**
* 得到真实的IP
*
* @return unknown
*/
function getRealIP(){
static $realip;
if (isset($_SERVER)){
if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])){
$realip = $_SERVER["HTTP_X_FORWARDED_FOR"];
} else if (isset($_SERVER["HTTP_CLIENT_IP"])) {
$realip = $_SERVER["HTTP_CLIENT_IP"];
} else {
$realip = $_SERVER["REMOTE_ADDR"];
}
} else {
if (getenv("HTTP_X_FORWARDED_FOR")){
$realip = getenv("HTTP_X_FORWARDED_FOR");
} else if (getenv("HTTP_CLIENT_IP")) {
$realip = getenv("HTTP_CLIENT_IP");
} else {
$realip = getenv("REMOTE_ADDR");
}
}
return $realip;
}
?>
</body>
<!--
<script type="text/javascript" src="http://ext.weather.com.cn/16670.js"></script>
-->
</html>