根据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文档,我 ...
随机推荐
- Golang, 以17个简短代码片段,切底弄懂 channel 基础
(原创出处为本博客:http://www.cnblogs.com/linguanh/) 前序: 因为打算自己搞个基于Golang的IM服务器,所以复习了下之前一直没怎么使用的协程.管道等高并发编程知识 ...
- C语言 · Anagrams问题
问题描述 Anagrams指的是具有如下特性的两个单词:在这两个单词当中,每一个英文字母(不区分大小写)所出现的次数都是相同的.例如,"Unclear"和"Nuclear ...
- 富文本编辑器Simditor的简易使用
最近打算自己做一个博客系统,并不打算使用帝国cms或者wordpress之类的做后台管理!自己处于学习阶段也就想把从前台到后台一起谢了.好了,废话不多说了,先来看看富文本编辑器SimDitor,这里是 ...
- jquery插件的用法之cookie 插件
一.使用cookie 插件 插件官方网站下载地址:http://plugins.jquery.com/cookie/ cookie 插件的用法比较简单,直接粘贴下面代码示例: //生成一个cookie ...
- angular2系列教程(七)Injectable、Promise、Interface、使用服务
今天我们要讲的ng2的service这个概念,和ng1一样,service通常用于发送http请求,但其实你可以在里面封装任何你想封装的方法,有时候控制器之间的通讯也是依靠service来完成的,让我 ...
- IE8/9 JQuery.Ajax 上传文件无效
IE8/9 JQuery.Ajax 上传文件有两个限制: 使用 JQuery.Ajax 无法上传文件(因为无法使用 FormData,FormData 是 HTML5 的一个特性,IE8/9 不支持) ...
- C++随笔:.NET CoreCLR之GC探索(1)
一直是.NET程序员,但是.NET的核心其实还是C++,所以我准备花 一点时间来研究CoreCLR和CoreFX.希望这个系列的文章能给大家带来 帮助. GC的代码有很多很多,而且结构层次对于一个初学 ...
- Newtonsoft.Json设置类的属性不序列化
参考页面: http://www.yuanjiaocheng.net/webapi/parameter-binding.html http://www.yuanjiaocheng.net/webapi ...
- 【干货分享】流程DEMO-离职流程
流程名: 离职申请 流程相关文件: 流程包.xml WebService业务服务.xml WebService.asmx WebService.cs 流程说明: 流程中集成了webservic ...
- 基于开源项目SharpMap的热力图(HeatLayer)实现。
当前公司需要一个用时较少的热力图呈现方案,在避免较底层的GDI开发和比较了多家GIS产品的实际效果之后,团队决定用sharpMap的API来实现,由于之前框架采用的是另外一个开源项目GMap.net, ...