- 相关推荐
php如何获取指定地区天气
导语:php如何获取指定地区天气呢?下面是小编给大家推荐的代码实现,大家可以参考阅读,更多详情请关注应届毕业生考试网。
PHP 获取指定地区的天气
在开发网站的时候用到天气查询,由于是基于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;
而对于:
$obj=json_decode($data);
它是把获取的json数据转化为一个对象,方便调用;
那么最后一句:
echo $obj->weatherinfo->city.':'.$obj->weatherinfo->weather1.' '.$obj->weatherinfo->temp1;
就是获取指定的数据并按照一定格式输出,
$obj->weatherinfo->city //城市
$obj->weatherinfo->weather1 //今天的天气
$obj->weatherinfo->temp1 //今天的气温
最后 在需要显示的地方
<?php include 'weather.php' ?>
即可。
【php如何获取指定地区天气】相关文章:
PHP如何获取表单07-27
PHP如何获取系统信息11-04
如何利用PHP语言获取连接信息10-01
php通过Chianz.com获取IP地址与地区的方法08-12
如何实现PHP获取表单数据与HTML嵌入PHP脚本09-23
如何使用php获取excel文件数据10-17
PHP获取星期的方法07-06
php创建cookie获取方法10-28
php怎么获取input的id09-10