php如何獲取指定地區天氣
來源:文萃谷 3.85K
導語:php如何獲取指定地區天氣呢?下面是小編給大家推薦的代碼實現,大家可以參考閲讀,更多詳情請關注應屆畢業生考試網。
PHP 獲取指定地區的天氣
在開發網站的時候用到天氣查詢,由於是基於Wordpress的 所以有很多限制,先建一個【】的文件,然後看代碼:
<?php
//獲取天氣
$url = '';
$id = '101181101'; //焦作的代號
$data = file_get_contents($url . $id .'');
$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 '' ?>
即可。