根据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℃ | |
| ) | |
| ) |
//获得天气数据,根据自己需求进行调整
?>
根据ip判断返回城市名称查询当地天气的更多相关文章
- ip获取所在城市名称等信息接口,及函数
函数: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 ...
- 通过jquery 获取用户当前所在的城市名称和IP地址
下面这段JS代码是通过jquery 结合新浪IP地址库和QQip地址库接口获取用户当前所在的城市(省份)名称. 用户当前IP地址等数据.其中当前IP是通过 QQip地址库接口获取,其他数据都是通过 新 ...
- python中通过客户端IP拿到所在城市和当地天气信息—附带项目案例
熟悉老一代QQ的小伙伴可能都知道,很早以前的QQ,鼠标滑到头像的位置,你的位置和IP会在详情页显示,那么这个是如何做到的呢?下面我们就来玩一玩这个东西 首先,需求分析: 1.拿到客户端IP 2.通过I ...
- 基于thinkphp实现根据用户ip判断地理位置并提供对应天气信息的应用
https://blog.csdn.net/MyCodeDream/article/details/46706469 我们都知道,在很多的网站都提供了给用户提供天气预报的功能,有时会发现,用户即使不输 ...
- ip获取到城市
<?phpfunction GetIP() { if ($_SERVER["HTTP_X_FORWARDED_FOR"]) $ip = $_SERVER[ ...
- C# 解析百度天气数据,Rss解析百度新闻以及根据IP获取所在城市
百度天气 接口地址:http://api.map.baidu.com/telematics/v3/weather?location=上海&output=json&ak=hXWAgbsC ...
- 获取客户端IP地址定位城市信息
获取客户端IP地址定位城市信息 1.首先获取客户端的IP地址 function getIPaddress(){ $IPaddress=''; if (isset($_SERVER)){ if (iss ...
- 获取ip地址及城市信息
大家好,今天给大家分享的是一个简单的知识获取登录用户的ip地址及城市信息,lz是一个小白,如果有哪些错误的地方 欢迎大家指出 东西很简单,直接上代码 [HttpPost] public string ...
- uniapp|微信小程序获取当前城市名称--逆地址解析
六年代码两茫茫,不思量,自难忘 6年资深前端主管一枚,只分享技术干货,项目实战经验 关注博主不迷路~ 问题 uniapp开发的小程序需要获取当前城市名称 解决步骤 看文档 当然是看uniapp文档,我 ...
随机推荐
- [APUE]UNIX进程的环境(下)
一.共享库 共享库使得可执行文件中不再需要包含常用的库函数,而只需在所有进程都可存取的存储区中保存这种库例程的一个副本.程序第一次执行的时候或第一次调用某个库函数的时候,用动态链接方法将程序与共享库函 ...
- StringMVC 中如何做数据校验
步骤一:引入四个jar包 步骤二:注册类型转换器 <context:component-scan base-package="cn.happy.controller"> ...
- 让 windows 下的命令行程序 cmd.exe 用起来更顺手
在 Windows 下使用 Larave 框架做开发,从 Composer 到 artisan 总是避免不了和 cmd.exe 打交道,系统默认的命令行界面却是不怎么好看,且每行显示的字符数是做了限制 ...
- Java学习之反射机制及应用场景
前言: 最近公司正在进行业务组件化进程,其中的路由实现用到了Java的反射机制,既然用到了就想着好好学习总结一下,其实无论是之前的EventBus 2.x版本还是Retrofit.早期的View注解框 ...
- Hawk 6. 编译和扩展开发
Hawk是开源项目,因此任何人都可以为其贡献代码.作者也非常欢迎使用者能够扩展出更有用的插件. 编译 编译需要Visual Stuido,版本建议使用2015, 2010及以上没有经过测试,但应该可以 ...
- 非关系型数据库(NoSql)
最近了解了一点非关系型数据库,刚刚接触,觉得这是一个很好的方向,对于大数据 方面的处理,非关系型数据库能起到至关重要的地位.这里我主要是整理了一些前辈的经验,仅供参考. 关系型数据库的特点 1.关系型 ...
- 设置line-height:1.5和line-height:150%或者line-height:150px的区别
直接正题: 看一下line-height可能的值: 其实可以分为两类: (1)不带单位的(如line-height:1.5),这种是推荐使用的: (2)带单位的(如line-heigth:30px/1 ...
- select、poll、epoll之间的区别总结
select.poll.epoll之间的区别总结 05/05. 2014 select,poll,epoll都是IO多路复用的机制.I/O多路复用就通过一种机制,可以监视多个描述符,一旦某个描述符就绪 ...
- 分布式学习系列【dubbo入门实践】
分布式学习系列[dubbo入门实践] dubbo架构 组成部分:provider,consumer,registry,monitor: provider,consumer注册,订阅类似于消息队列的注册 ...
- Javascript中的valueOf与toString
基本上,javascript中所有数据类型都拥有valueOf和toString这两个方法,null除外.它们俩解决javascript值运算与显示的问题,本文将详细介绍,有需要的朋友可以参考下. t ...