- 相關推薦
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