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í)候用到天氣查詢(xún),由于是基于Wordpress的 所以有很多限制,先建一個(gè)【weather.PHP】的文件,然后看代碼:
<?php
/pic/p>
$url = '/pic/data/';
$id = '101181101'; /pic/p>
$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 /pic/p>
$obj->weatherinfo->weather1 /pic/p>
$obj->weatherinfo->temp1 /pic/p>
最后 在需要顯示的地方
<?php include 'weather.php' ?>
即可。
【php如何獲取指定地區(qū)天氣】相關(guān)文章:
PHP如何獲取表單10-26
PHP如何獲取系統(tǒng)信息02-02
如何利用PHP語(yǔ)言獲取連接信息11-04
php通過(guò)Chianz.com獲取IP地址與地區(qū)的方法11-18
如何實(shí)現(xiàn)PHP獲取表單數(shù)據(jù)與HTML嵌入PHP腳本01-16
如何使用php獲取excel文件數(shù)據(jù)08-26
PHP獲取星期的方法08-08
php怎么獲取input的id08-25