PHP 获取指定地区的天气
在开发网站的时候用到天气查询,由于是基于Wordpress的 所以有很多限制,先建一个【weather.PHP】的文件,然后看代码:
?
|
1
2
3
4
5
6
7
8
|
<?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;
|
对于:
?
|
1
2
3
|
$url = 'http://m.weather.com.cn/data/';
$id = '101181101'; //焦作的代号
$data = file_get_contents($url . $id .'.html');
|
可简写为:
?
|
1
|
$data = file_get_contents('http://m.weather.com.cn/data/101181101.html');
|
而对于:
?
|
1
|
$obj=json_decode($data);
|
它是把获取的json数据转化为一个对象,方便调用;
那么最后一句:
?
|
1
|
echo $obj->weatherinfo->city.':'.$obj->weatherinfo->weather1.' '.$obj->weatherinfo->temp1;
|
就是获取指定的数据并按照一定格式输出,
?
|
1
2
3
|
$obj->weatherinfo->city //城市
$obj->weatherinfo->weather1 //今天的天气
$obj->weatherinfo->temp1 //今天的气温
|
最后 在需要显示的地方
?
|
1
|
<?php include 'weather.php' ?>
|
即可。
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
相关文章
猜你喜欢
- ASP.NET自助建站系统中如何实现多语言支持? 2025-06-10
- 64M VPS建站:如何选择最适合的网站建设平台? 2025-06-10
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
- ASP.NET自助建站系统的数据库备份与恢复操作指南 2025-06-10
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
攻击者是如何将PHP Phar包伪装成图像以绕过文件类型检测的(推荐)
2025-05-29 106 -
2025-05-29 91
-
2025-05-29 103
-
2025-05-29 95
-
2025-06-04 27
热门评论

