PHP獲取指定地區的天氣

2021-06-21 21:48:46 字數 1183 閱讀 6105

在開發**的時候用到天氣查詢,由於是基於wordpress

的 所以有很多限制,先建乙個【

weather.php】

的檔案,然後看**:

<?php 

//獲取天氣

$url = '';

$id = '101181101'; //焦作的代號

$data = file_get_contents($url . $id .'.html');

$obj=json_decode($data);

echo $obj->weatherinfo->city.':'.$obj->weatherinfo->weather1.' '.$obj->weatherinfo->temp1;

對於:

$url = '';  

$id = '101181101'; //焦作的代號

$data = file_get_contents($url . $id .'.html');

可簡寫為:

$data = file_get_contents('101181101.html');

而對於:

$obj=json_decode($data);

它是把獲取的json資料轉化為乙個物件,方便呼叫;

那麼最後一句:

echo $obj->weatherinfo->city.':'.$obj->weatherinfo->weather1.'  '.$obj->weatherinfo->temp1;

就是獲取指定的資料並按照一定格式輸出,

$obj->weatherinfo->city //城市

$obj->weatherinfo->weather1 //今天的天氣

$obj->weatherinfo->temp1 //今天的氣溫

其他引數可以自行訪問 101181101.html 檢視

最後 在需要顯示的地方

<?php include 'weather.php' ?>

即可。

PHP獲取IP所在地區

這是乙個php獲取客戶端ip所在地區的類,它能根據ip位址查地區,簡單實用。使用示例 示例一 iplocation new iplocation client iplocation getlocation print r client 示例二 iplocation new iplocation qq...

PHP獲取IP所在地區

1.獲取ip位址的api ip位址庫api 位址字串 根據訪問者ip顯示對應地區的特定內容 根據訪問者ip,來讀ip庫或介面獲取地區,用該地區做篩選查詢 純真ip資料庫檔案版 示例一 iplocation new iplocation client iplocation getlocation pr...

php 根據ip位址獲取地區資訊

public static function get info ip public static function substr11 str x u str,regs preg match all 正規表示式 擷取的字串 成功之後返回的結果集 是陣列 s join regs 0 join 可選。規定...