根据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文档,我 ...
随机推荐
- ES6模块import细节
写在前面,目前浏览器对ES6的import支持还不是很好,需要用bable转译. ES6引入外部模块分两种情况: 1.导入外部的变量或函数等: import {firstName, lastName, ...
- 【SQLServer】记一次数据迁移-标识重复的简单处理
汇总篇:http://www.cnblogs.com/dunitian/p/4822808.html#tsql 今天在数据迁移的时候因为手贱遇到一个坑爹问题,发来大家乐乐,也传授新手点经验 迁移惯用就 ...
- JavaScript动画-碰撞检测
▓▓▓▓▓▓ 大致介绍 碰撞检测是指在页面中有多个元素时,拖拽一个元素会出现碰撞问题,碰撞检测是以模拟拖拽和磁性吸附中的范围限定为基础的 效果:碰撞检测 ▓▓▓▓▓▓ 碰撞检测 先来看看碰撞检测的原理 ...
- 【Big Data】HADOOP集群的配置(一)
Hadoop集群的配置(一) 摘要: hadoop集群配置系列文档,是笔者在实验室真机环境实验后整理而得.以便随后工作所需,做以知识整理,另则与博客园朋友分享实验成果,因为笔者在学习初期,也遇到不少问 ...
- 【iOS】Xcode8+Swift3 纯代码模式实现 UICollectionView
开发环境 macOS Sierra 10.12.Xcode 8.0,如下图所示: 总体思路 1.建立空白的storyboard用于呈现列表 2.实现自定义单个单元格(继承自:UICollectionV ...
- WPF 普通属性变化通知
问题描述:使用ObservableCollection<OrderItem> source 给Datagrid.ItemsSource赋值,在后台更新source集合后,前台Datagri ...
- StatePattern(状态模式)
/** * 状态模式 * @author TMAC-J * 状态模式和策略模式很像,其实仔细研究发现完全不一样 * 策略模式各策略之间没有任何关系,独立的 * 状态模式各状态之间接口方法都是一样的 * ...
- BI分析受阻?FineBI推出SPA螺旋式分析新功能!
过去,企业级的数据分析通常会有这么几种场景,业务部门托信息部门分析数据,结果报表一出,唇枪舌剑争论你我高低,数据不准,指标不对.信息部门欠缺业务概念,业务部门不懂技术逻辑,数据分析之路,暂时搁浅. 后 ...
- “风投云涌”:那些被资本看中的IT企业的风光与辛酸
进入七月份以来,纷享销客获得D轮融资1亿美元,撼动业界,资本与IT联姻令一部分创业者眼红的同时,没有人注意到背后的风险. 科技与资本的结合,是当今经济社会前行的宏大主题.相关统计显示,软件行 ...
- Android Studio-—使用OpenCV的配置方法和demo以及开发过程中遇到的问题解决
前提: 1.安装Android Studio(过程略) 2.官网下载OpenCV for Android 网址:http:opencv.org/downloads.html 我下载的是下图的版本 3. ...