《PHP學(xué)習(xí):PHP 獲取指定地區(qū)的天氣實(shí)例代碼》要點(diǎn):
本文介紹了PHP學(xué)習(xí):PHP 獲取指定地區(qū)的天氣實(shí)例代碼,希望對(duì)您有用。如果有疑問,可以聯(lián)系我們。
PHP 獲取指定地區(qū)的天氣PHP實(shí)例
在開發(fā)網(wǎng)站的時(shí)候用到天氣查詢,由于是基于Wordpress的 所以有很多限制,先建一個(gè)【weather.PHP】的文件,然后看代碼:
PHP實(shí)例
<?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ì)于:
PHP實(shí)例
$url = 'http://m.weather.com.cn/data/'; $id = '101181101'; //焦作的代號(hào) $data = file_get_contents($url . $id .'.html');
可簡(jiǎn)寫為:
PHP實(shí)例
$data = file_get_contents('http://m.weather.com.cn/data/101181101.html');
而對(duì)于:
PHP實(shí)例
$obj=json_decode($data);
它是把獲取的json數(shù)據(jù)轉(zhuǎn)化為一個(gè)對(duì)象,方便調(diào)用;
PHP實(shí)例
那么最后一句:
PHP實(shí)例
echo $obj->weatherinfo->city.':'.$obj->weatherinfo->weather1.' '.$obj->weatherinfo->temp1;
就是獲取指定的數(shù)據(jù)并按照一定格式輸出,
PHP實(shí)例
$obj->weatherinfo->city //城市 $obj->weatherinfo->weather1 //今天的天氣 $obj->weatherinfo->temp1 //今天的氣溫
最后 在需要顯示的地方
PHP實(shí)例
<?php include 'weather.php' ?>
即可.PHP實(shí)例
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!PHP實(shí)例
轉(zhuǎn)載請(qǐng)注明本頁網(wǎng)址:
http://www.snjht.com/jiaocheng/1840.html