搜尋
首頁php教程php手册根據ip判斷返回城市名稱查詢當地天氣

header("content-type:text/html;charset=utf-8");
date_default_timezone_set("Asia/Shanghai");
error_reporting(0);
// 根據IPanghai");
error_reporting(0);
// 依照這個城市
user_ipip = $_SERVER['REMOTE_ADDR'];
$url ="http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=$user_ip";

$address = file_get_contents($url );

$address_arr = json_decode($address);  //返回對象,需要轉換為陣列

//以上海為例stdClass Object([ret] => 1[start] => -1[end] => -1[country] => 中國[province] => 上海[city] => 上海[district] =>[isp] =>[type] =>[desc] =>)
 
 
 
 
 
 
 
 
 
 
 
 



function object_array($array){
  if(is_object($array)){
    $array = (array)$array;🀜    $array = (array)$array;🀜   『  foreach($array as $key=> $value){
      $array[$key] = object_array($value);🀎     }
 〜}
   }
 〜}
 〜returnreturns $
//透過此函數轉換為陣列

$address_arr = object_array($address_arr);


// print_r($address_arr);

Array            


$city = $address_arr["city"];  

//輸出為上海,取得城市透過百度天氣API查詢當地天氣

$con=file_get_contents("http://api.map.baidu.com/telematics/v3/weather?location=$city&output=json&ak=spmMww7Eoqcmf3FXbnLyDUwL");  ////注意ak值需要進入百度登錄機碼,附加位址:http://lbsyun.baidu.com/apiconsole/key
$con = json_decode($con);

print_r($con);

(
[ret] => 1
[start] => -1
[end] => -1
[country] => 中國
[province] => 上海
[city] => 上海
[district] =>
[isp] =>
[type] =>
[desc] =>
)
stdClass Object
  (
  [error] => 0
  [status] => success
  [date] => 2016-09-23
  [results] => Array
  (
  [0] => stdClass Object
  (
  [currentCity] => 上海
  [pm25] => 42
  [index] => Array
  (
  [0] => stdClass Object
  (
  [title] => 穿衣
  [zs] => 熱
  [tipt] => 穿衣指數
  [des] => 天氣熱,建議短裙、短褲、短薄外套、T恤等夏季服裝。
  )
   
  [1] => stdClass Object
  (
  [title] => 洗車
  [zs] => 較適合
  [tipt] => 洗車指數
  [des] => 較適合洗車,未來一天無雨,風力較小,擦洗一新的汽車至少能維持一天。
  )
   
  [2] => stdClass Object
  (
  [title] => 旅遊
  [zs] => 適宜
  [tipt] => 旅遊指數
  [des] => 天氣較好,但絲毫不會影響您出行的心情。溫度適宜又有微風相伴,適合旅遊。
  )
   
  [3] => stdClass Object
  (
  [title] => 感冒
  [zs] => 少發
  [tipt] => 感冒指數
  [des] => 各項氣象條件適宜,無明顯降溫過程,發生感冒機率較低。
  )
   
  [4] => stdClass Object
  (
  [title] => 運動
  [zs] => 較適合
  [tipt] => 運動指數
  [des] => 天氣較好,戶外運動請注意防曬。推薦您進行室內運動。
  )
   
  [5] => stdClass Object
  (
  [title] => 紫外線強度
  [zs] => 弱
  [tipt] => 紫外線強度指數
  [des] => 紫外線強度較弱,建議出門前塗抹SPF在12-15之間、PA+的防曬保養品。
  )
   
  )
   
  [weather_data] => Array
  (
  [0] => stdClass Object
  (
  [date] => 週五 09月23日 (即時:26℃)
  [dayPictureUrl] => http://api.map.baidu.com/images/weather/day/duoyun.png
  [nightPictureUrl] => http://api.map.baidu.com/images/weather/night/duoyun.png
  [weather] => 多雲
  [wind] => 東風微風
  [temperature] => 27 ~ 21℃
  )
   
  [1] => stdClass Object
  (
  [date] => 週六
  [dayPictureUrl] => http://api.map.baidu.com/images/weather/day/duoyun.png
  [nightPictureUrl] => http://api.map.baidu.com/images/weather/night/duoyun.png
  [weather] => 多雲
  [wind] => 東風微風
  [temperature] => 28 ~ 23℃
  )
   
  [2] => stdClass Object
  (
  [date] => 週日
  [dayPictureUrl] => http://api.map.baidu.com/images/weather/day/duoyun.png
  [nightPictureUrl] => http://api.map.baidu.com/images/weather/night/yin.png
  [weather] => 多雲轉陰
  [wind] => 東風微風
  [temperature] => 28 ~ 23℃
  )
   
  [3] => stdClass Object
  (
  [date] => 週一
  [dayPictureUrl] => http://api.map.baidu.com/images/weather/day/duoyun.png
  [nightPictureUrl] => http://api.map.baidu.com/images/weather/night/yin.png
  [weather] => 多雲轉陰
  [wind] => 東北風微風
  [temperature] => 29 ~ 25℃
  )
   
 
   
 
   
 
   
 

$arr = object_array($con);  //繼續轉換成陣列運算

$detail = $arr["results"][0]["weather_data"];
$city = $arr["results"][0]["currentCity"];

print_r($detail);

Array
  (
  [0] => Array
  (
  [date] => 週五 09月23日 (即時:26℃)
  [dayPictureUrl] => http://api.map.baidu.com/images/weather/day/duoyun.png
  [nightPictureUrl] => http://api.map.baidu.com/images/weather/night/duoyun.png
  [weather] => 多雲
  [wind] => 東風微風
  [temperature] => 27 ~ 21℃
  )
   
  [1] => Array
  (
  [date] => 週六
  [dayPictureUrl] => http://api.map.baidu.com/images/weather/day/duoyun.png
  [nightPictureUrl] => http://api.map.baidu.com/images/weather/night/duoyun.png
  [weather] => 多雲
  [wind] => 東風微風
  [temperature] => 28 ~ 23℃
  )
   
  [2] => Array
  (
  [date] => 週日
  [dayPictureUrl] => http://api.map.baidu.com/images/weather/day/duoyun.png
  [nightPictureUrl] => http://api.map.baidu.com/images/weather/night/yin.png
  [weather] => 多雲轉陰
  [wind] => 東風微風
  [temperature] => 28 ~ 23℃
  )
   
  [3] => Array
  (
  [date] => 週一
  [dayPictureUrl] => http://api.map.baidu.com/images/weather/day/duoyun.png
  [nightPictureUrl] => http://api.map.baidu.com/images/weather/night/yin.png
  [weather] => 多雲轉陰
  [wind] => 東北風微風
  [temperature] => 29 ~ 25℃
  )
   
  )

//取得天氣數據,依照自己需求進行調整

?>

 

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

將Eclipse與SAP NetWeaver應用伺服器整合。

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

mPDF

mPDF

mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),