- 相關(guān)推薦
php如何獲取指定地區(qū)天氣
導(dǎo)語(yǔ):php如何獲取指定地區(qū)天氣呢?下面是小編給大家推薦的代碼實(shí)現(xiàn),大家可以參考閱讀,更多詳情請(qǐng)關(guān)注應(yīng)屆畢業(yè)生考試網(wǎng)。
PHP 獲取指定地區(qū)的天氣
在開(kāi)發(fā)網(wǎng)站的時(shí)候用到天氣查詢,由于是基于Wordpress的 所以有很多限制,先建一個(gè)【weather.PHP】的文件,然后看代碼:
<?php
//獲取天氣
$url = 'http://m.weather.com.cn/data/';
$id = '101181101'; //焦作的代號(hào)
$data = file_get_contents($url . $id .'.html');
$obj=json_decode($data);
echo $obj->weatherinfo->city.':'.$obj->weatherinfo->weather1.' '.$obj->weatherinfo->temp1;
而對(duì)于:
$obj=json_decode($data);
它是把獲取的json數(shù)據(jù)轉(zhuǎn)化為一個(gè)對(duì)象,方便調(diào)用;
那么最后一句:
echo $obj->weatherinfo->city.':'.$obj->weatherinfo->weather1.' '.$obj->weatherinfo->temp1;
就是獲取指定的數(shù)據(jù)并按照一定格式輸出,
$obj->weatherinfo->city //城市
$obj->weatherinfo->weather1 //今天的天氣
$obj->weatherinfo->temp1 //今天的氣溫
最后 在需要顯示的地方
<?php include 'weather.php' ?>
即可。
【php如何獲取指定地區(qū)天氣】相關(guān)文章:
PHP如何獲取表單07-27
PHP如何獲取系統(tǒng)信息11-04
如何利用PHP語(yǔ)言獲取連接信息10-01
php通過(guò)Chianz.com獲取IP地址與地區(qū)的方法08-12
如何實(shí)現(xiàn)PHP獲取表單數(shù)據(jù)與HTML嵌入PHP腳本09-23
如何使用php獲取excel文件數(shù)據(jù)10-17
PHP獲取星期的方法07-06
php怎么獲取input的id09-10