您好,欢迎访问一九零五行业门户网

根据ip判断返回城市名称查询当地天气

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℃
  )
)
//获得天气数据,根据自己需求进行调整
?>
其它类似信息

推荐信息