根据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文档,我 ...
随机推荐
- 01.SQLServer性能优化之---水平分库扩展
汇总篇:http://www.cnblogs.com/dunitian/p/4822808.html#tsql 第一次引入文件组的概念:http://www.cnblogs.com/dunitian/ ...
- 初识Hadoop、Hive
2016.10.13 20:28 很久没有写随笔了,自打小宝出生后就没有写过新的文章.数次来到博客园,想开始新的学习历程,总是被各种琐事中断.一方面确实是最近的项目工作比较忙,各个集群频繁地上线加多版 ...
- ASP.NET MVC 请求路径相关参数的获取
Request.ApplicationPath / Request.CurrentExecutionFilePath /Home/Index Request.FilePath /Home/Index ...
- ASP.NET Aries 入门开发教程4:查询区的下拉配置
背景: 今天去深圳溜达了一天,刚回来,看到首页都是微软大法好,看来离.NET的春天就差3个月了~~ 回到正题,这篇的教程讲解下拉配置. 查询区的下拉配置: 1:查询框怎么配置成下拉? 在配置表头:格式 ...
- HTML5新特性有哪些,你都知道吗
一.画布(Canvas) 画布是网页中的一块区域,可所以用JavaScript在上面绘图.下面我们来创建一个画布并在上面绘制一个坦克(后面将用HTML5做一个坦克大战游戏),代码如下: <!DO ...
- JS与APP原生控件交互
"热更新"."热部署"相信对于混合式开发的童鞋一定不陌生,那么APP怎么避免每次升级都要在APP应用商店发布呢?这里就用到了混合式开发的概念,对于电商网站尤其显 ...
- Xamarin与Visual stuido2015离线安装包分享
最近看见大伙留言才知道国内安装Xamarin开发原来这么艰辛啊! 第一:网速不快 第二:Android SDK下载受限 等等... 鉴于这些原因,特写下这篇文章以及分享打包好的离线包以帮助大家尽快体验 ...
- ubuntu如何安装nodejs最新版 本
如何正确的安装nodejs? 我们可以先安装nvm, git clone https://github.com/creationix/nvm.git ~/.nvm 然后打开 ~/.bashrc , ...
- Adaboost提升算法从原理到实践
1.基本思想: 综合某些专家的判断,往往要比一个专家单独的判断要好.在"强可学习"和"弱科学习"的概念上来说就是我们通过对多个弱可学习的算法进行"组合 ...
- 设计模式C#合集--工厂方法模式
简单工厂,代码: public interface ISpeak { public void Say(); } public class Hello : ISpeak { public void Sa ...