php教程 php手册 IP 판단을 기반으로 지역 날씨를 쿼리하려면 도시 이름을 반환합니다.

IP 판단을 기반으로 지역 날씨를 쿼리하려면 도시 이름을 반환합니다.

Sep 24, 2016 am 09:02 AM

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); // 객체를 반환하고 배열로 변환해야 함

//상하이를 예로 들어

함수 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);

stdClass Object
(
[ret] => 1

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


$city = $address_arr["city"]

//출력은 상하이이며, 획득한 도시는 Baidu Weather API를 통해 현지 날씨를 쿼리할 수 있습니다

$con=file_get_contents("http://api.map.baidu.com/telematics/v3/weather?location=$city&output=json&ak=spmMww7Eoqcmf3FXbnLyDUwL") //ak 값은 다음을 통해 등록해야 합니다. Baidu 인터페이스, 첨부된 주소: http://lbsyun.baidu.com/apiconsole/key
$con = json_decode($con);

print_r($con);

stdClass 객체
  (
  [오류] => 0
  [상태] => 성공
  [날짜] => 2016-09-23
  [결과] => 배열
  (
  [0] => stdClass 객체
  (
  [현재 도시] => 상해
  [pm25] => 42
  [색인] => 배열
  (
  [0] => stdClass 객체
  (
  [제목] => 穿衣
  [zs] => 덥다
  [팁] => 穿衣指数
  [des] => 天气热,建议着短裙、短裤、短薄外套、T恤等夏季服装。
  )
   
  [1] => stdClass 객체
  (
  [제목] => 洗车
  [zs] => 较适宜
  [팁] => 洗车指数
  [des] => 较适宜洗车,未来一天无雨,风力较小,擦洗一新的汽车至少能保持一天。
  )
   
  [2] => stdClass 객체
  (
  [제목] => 旅游
  [zs] => 适宜
  [팁] => 旅游指数
  [des] => 天气较好,但丝毫不会影响您丝游。温島适宜又有微风游,适宜旅游。
  )
   
  [3] => stdClass 객체
  (
  [제목] => ㅋㅋㅋ
  [zs] => 少发
  [팁] => 感冒指数
  [des] => 各项气象条件适宜,无明显降温过程,发生感冒机率较低。
)
[4] => stdClass 객체
(
[제목] => 스포츠
[zs] => 더 적합함
[tipt] => 스포츠지수
[des] => 날씨가 좋은데, 야외 운동 시 자외선 차단에 유의하시기 바랍니다. 실내운동을 권장합니다.
)
[5] => stdClass 객체
(
[제목] => 자외선 강도
[zs] => 약함
[tipt] => 자외선 강도 지수
[des] => 자외선의 강도가 약하기 때문에 외출 전 SPF 12~15 PA+의 자외선 차단 스킨케어 제품을 바르는 것이 좋습니다.
)
)
[weather_data] => 배열
(
[0] => stdClass 객체
(
[날짜] => 9월 23일 금요일(실시간: 26℃)
[dayPictureUrl] => http://api.map.baidu.com/images/weather/day/duoyun.png
[nightPictureUrl] => http://api.map.baidu.com/images/weather/night/duoyun.png
[날씨] => 흐림
[바람] => 동풍
[온도] => 27~21℃
)
[1] => stdClass 객체
(
[날짜] => 토요일
[dayPictureUrl] => http://api.map.baidu.com/images/weather/day/duoyun.png
[nightPictureUrl] => http://api.map.baidu.com/images/weather/night/duoyun.png
[날씨] => 흐림
[바람] => 동풍
[온도] => 28~23℃
)
[2] => stdClass 객체
(
[날짜] => 일요일
[dayPictureUrl] => http://api.map.baidu.com/images/weather/day/duoyun.png
[nightPictureUrl] => http://api.map.baidu.com/images/weather/night/yin.png
[날씨] => 흐림~흐림
[바람] => 동풍
[온도] => 28~23℃
)
[3] => stdClass 객체
(
[날짜] => 월요일
[dayPictureUrl] => http://api.map.baidu.com/images/weather/day/duoyun.png
[nightPictureUrl] => http://api.map.baidu.com/images/weather/night/yin.png
[날씨] => 흐림~흐림
[바람] => 북동풍
[온도] => 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 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

AI Hentai Generator

AI Hentai Generator

AI Hentai를 무료로 생성하십시오.

인기 기사

R.E.P.O. 에너지 결정과 그들이하는 일 (노란색 크리스탈)
4 몇 주 전 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 최고의 그래픽 설정
4 몇 주 전 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 아무도들을 수없는 경우 오디오를 수정하는 방법
4 몇 주 전 By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25 : Myrise에서 모든 것을 잠금 해제하는 방법
1 몇 달 전 By 尊渡假赌尊渡假赌尊渡假赌

뜨거운 도구

메모장++7.3.1

메모장++7.3.1

사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전

SublimeText3 중국어 버전

중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기

스튜디오 13.0.1 보내기

강력한 PHP 통합 개발 환경

드림위버 CS6

드림위버 CS6

시각적 웹 개발 도구

SublimeText3 Mac 버전

SublimeText3 Mac 버전

신 수준의 코드 편집 소프트웨어(SublimeText3)