根据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文档,我 ...
随机推荐
- iOS总结_UI层自我复习总结
UI层复习笔记 在main文件中,UIApplicationMain函数一共做了三件事 根据第三个参数创建了一个应用程序对象 默认写nil,即创建的是UIApplication类型的对象,此对象看成是 ...
- node服务的监控预警系统架构
需求背景 目前node端的服务逐渐成熟,在不少公司内部也开始承担业务处理或者视图渲染工作.不同于个人开发的简单服务器,企业级的node服务要求更为苛刻: 高稳定性.高可靠性.鲁棒性以及直观的监控和报警 ...
- jQuery学习之路(1)-选择器
▓▓▓▓▓▓ 大致介绍 终于开始了我的jQuery学习之路!感觉不能再拖了,要边学习原生JavaScript边学习jQuery jQuery是什么? jQuery是一个快速.简洁的JavaScript ...
- [C#] 简单的 Helper 封装 -- SQLiteHelper
using System; using System.Data; using System.Data.SQLite; namespace SqliteConsoleApp { /// <summ ...
- PHP类和对象之重载
PHP中的重载指的是动态的创建属性与方法,是通过魔术方法来实现的.属性的重载通过__set,__get,__isset,__unset来分别实现对不存在属性的赋值.读取.判断属性是否设置.销毁属性. ...
- 《LoadRunner12七天速成宝典》来了
看到自己的新书又要发行了,算算从09年第一本书开始,不知不觉已经是第四本书了(帮朋友合写的书不算),每次写完之后都会说太累了,不想再写了,但是却又次次反悔,吞下食言的苦果.如果非要说第四本书的感受,那 ...
- gulp初学
原文地址:gulp初学 至于gulp与grunt的区别,用过的人都略知一二,总的来说就是2点: 1.gulp的gulpfile.js 配置简单而且更容易阅读和维护.之所以如此,是因为它们的工作方式不 ...
- 防线修建 bzoj 2300
防线修建(1s 512MB)defense [问题描述] 近来A国和B国的矛盾激化,为了预防不测,A国准备修建一条长长的防线,当然修建防线的话,肯定要把需要保护的城市修在防线内部了.可是A国上层现在还 ...
- 微信公众号开发(一)--验证服务器地址的Java实现
现在主流上都用php写微信公众号后台,其实作为后端语言之一的java也可以实现. 这篇文章将对验证服务器地址这一步做出实现. 参考资料:1.慕课网-<初识java微信公众号开发>,2.微信 ...
- js 基础篇(点击事件轮播图的实现)
轮播图在以后的应用中还是比较常见的,不需要多少行代码就能实现.但是在只掌握了js基础知识的情况下,怎么来用较少的而且逻辑又简单的方法来实现呢?下面来分析下几种不同的做法: 1.利用位移的方法来实现 首 ...