国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

首頁 > php教程 > php手冊 > 正文

根據ip判斷返回城市名稱查詢當地天氣

php中文網
發(fā)布: 2016-09-24 09:02:50
原創(chuàng)
1837人瀏覽過

header("content-type:text/html;charset=utf-8");
date_default_timezone_set("asia/shanghai");
error_reporting(0);
// 根據ip判斷城市
$user_ip = $_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;
  }
  if(is_array($array)){
    foreach($array as $key=>$value){
      $array[$key] = object_array($value);
    }
  }
  return $array;
}

//通過該函數轉化為數組
$address_arr = object_array($address_arr);

// print_r($address_arr);

Array
? (
? [ret] => 1
? [start] => -1
? [end] => -1
? [country] => 中國
? [province] => 上海
? [city] => 上海
? [district] =>
? [isp] =>
? [type] =>
? [desc] =>
? )


$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);

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] => 少發(fā)
? [tipt] => 感冒指數
? [des] => 各項氣象條件適宜,無明顯降溫過程,發(fā)生感冒機率較低。
? )
? ?
? [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); ?//繼續(xù)轉成數組操作

$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℃
? )
? ?
? )

//獲得天氣數據,根據自己需求進行調整

?>

?

最佳 Windows 性能的頂級免費優(yōu)化軟件
最佳 Windows 性能的頂級免費優(yōu)化軟件

每個人都需要一臺速度更快、更穩(wěn)定的 PC。隨著時間的推移,垃圾文件、舊注冊表數據和不必要的后臺進程會占用資源并降低性能。幸運的是,許多工具可以讓 Windows 保持平穩(wěn)運行。

下載
來源:php中文網
本文內容由網友自發(fā)貢獻,版權歸原作者所有,本站不承擔相應法律責任。如您發(fā)現(xiàn)有涉嫌抄襲侵權的內容,請聯(lián)系admin@php.cn
最新問題
開源免費商場系統(tǒng)廣告
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關于我們 免責申明 意見反饋 講師合作 廣告合作 最新更新
php中文網:公益在線php培訓,幫助PHP學習者快速成長!
關注服務號 技術交流群
PHP中文網訂閱號
每天精選資源文章推送
PHP中文網APP
隨時隨地碎片化學習
PHP中文網抖音號
發(fā)現(xiàn)有趣的

Copyright 2014-2025 http://www.miracleart.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號