PHP获取指定地区的天气
2024-08-27 14:04:10
在开发网站的时候用到天气查询,由于是基于Wordpress的 所以有很多限制,先建一个【weather.php】的文件,然后看代码:
<?php
//获取天气
$url = 'http://m.weather.com.cn/data/';
$id = '101181101'; //焦作的代号
$data = file_get_contents($url . $id .'.html'); $obj=json_decode($data);
echo $obj->weatherinfo->city.':'.$obj->weatherinfo->weather1.' '.$obj->weatherinfo->temp1;
对于:
$url = 'http://m.weather.com.cn/data/';
$id = '101181101'; //焦作的代号
$data = file_get_contents($url . $id .'.html');
可简写为:
$data = file_get_contents('http://m.weather.com.cn/data/101181101.html');
而对于:
$obj=json_decode($data);
它是把获取的json数据转化为一个对象,方便调用;
那么最后一句:
echo $obj->weatherinfo->city.':'.$obj->weatherinfo->weather1.' '.$obj->weatherinfo->temp1;
就是获取指定的数据并按照一定格式输出,
$obj->weatherinfo->city //城市
$obj->weatherinfo->weather1 //今天的天气
$obj->weatherinfo->temp1 //今天的气温
其他参数可以自行访问 http://m.weather.com.cn/data/101181101.html 查看
最后 在需要显示的地方
<?php include 'weather.php' ?>
即可。
最新文章
- Oracle中varchar,varchar2,nvarchar,nvarchar2的区别及其它数据类型描述
- mybatis如何做分页处理
- Hive函数大全
- Linux主机共享目录给Windows主机的方法
- iOS安全—阻止tweak注入hook api
- zoj 3229 Shoot the Bullet(无源汇上下界最大流)
- wpf集成unity
- 【程序员小助手】Synergy,感受穿越屏幕之美
- 遍历Javascript数组的一种方法!
- 国内不能使用Google解决方案(不断更新与递增中...)
- Docker(十):Docker安全
- MFC的两个问题
- 使用Ncat反弹Shell
- 10个JavaScript常见BUG及修复方法
- struts2 对EL的改变
- Luogu P2827 蚯蚓
- dp 优化 F2. Pictures with Kittens (hard version)
- mongodb 最佳可视化工具mongobooster
- golang优先队列
- AWS系列-创建AMI
热门文章
- P2P系统,一致性哈希和DHT
- Hive-RCFile文件存储格式
- 14 ListFragment 代码案例
- [ExtJS5学习笔记]第二十四节 Extjs5中表格gridpanel或者表单数据后台传输remoteFilter设置
- EBS系统克隆
- linux中echo的用法
- 【一天一道LeetCode】#371. Sum of Two Integers
- 还在繁琐的敲MVP接口和实现类吗,教你一秒搞定。
- Arquillian Exception:java.lang.NoClassDefFoundError
- [sersync+rsync] centos6.5 远程文件同步部署记录