搜索
首页php教程php手册根据ip判断返回城市名称查询当地天气

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℃
  )
   
  )

//获得天气数据,根据自己需求进行调整

?>

 

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
3 周前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解锁Myrise中的所有内容
4 周前By尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SecLists

SecLists

SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

WebStorm Mac版

WebStorm Mac版

好用的JavaScript开发工具