현재 날씨 가져오는 부분

매출이 오르면 내리는 수수료! 지금 수수료센터에서 전자결제(PG)수수료 비교견적 신청해 보세요!
현재 날씨 가져오는 부분

QA

현재 날씨 가져오는 부분

본문

$url = "https://api.openweathermap.org/data/2.5/forecast?lat=35.17332899587668&lon=129.13085319903072&lang=en&units=metric&appid=appkey";
$ch = curl_init();

curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);
curl_close($ch);

$jsondata = json_decode($response);
echo '<pre>';
print_r($jsondata);
echo '#';
echo '<bR>';
위와 같이 작업해서 웹에서 보면 아래와 같이 출력이 됩니다.
여기에서 첫번째 부분의 온도 [temp] => 24.44  를 가져오려고 하면 
echo 어떻게 호출을 해야될까요?

stdClass Object
(
    [cod] => 200
    [message] => 0
    [cnt] => 40
    [list] => Array
        (
            [0] => stdClass Object
                (
                    [dt] => 1654257600
                    [main] => stdClass Object
                        (
                            [temp] => 24.44
                            [feels_like] => 24.11
                            [temp_min] => 19.43
                            [temp_max] => 24.44
                            [pressure] => 1011
                            [sea_level] => 1011
                            [grnd_level] => 1011
                            [humidity] => 45
                            [temp_kf] => 5.01
                        )

                    [weather] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [id] => 800
                                    [main] => Clear
                                    [description] => clear sky
                                    [icon] => 01n
                                )

                        )

                    [clouds] => stdClass Object
                        (
                            [all] => 8
                        )

                    [wind] => stdClass Object
                        (
                            [speed] => 5.79
                            [deg] => 36
                            [gust] => 9.08
                        )

                    [visibility] => 10000
                    [pop] => 0
                    [sys] => stdClass Object
                        (
                            [pod] => n
                        )

                    [dt_txt] => 2022-06-03 12:00:00
                )

            [1] => stdClass Object
                (
                    [dt] => 1654268400
                    [main] => stdClass Object
                        (
                            [temp] => 21.1
                            [feels_like] => 20.91
                            [temp_min] => 18.18
                            [temp_max] => 21.1
                            [pressure] => 1012
                            [sea_level] => 1012
                            [grnd_level] => 1012
                            [humidity] => 63
                            [temp_kf] => 2.92
                        )

                    [weather] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [id] => 802
                                    [main] => Clouds
                                    [description] => scattered clouds
                                    [icon] => 03n
                                )

                        )

                    [clouds] => stdClass Object
                        (
                            [all] => 35
                        )

                    [wind] => stdClass Object
                        (
                            [speed] => 4.82
                            [deg] => 27
                            [gust] => 7.32
                        )

                    [visibility] => 10000
                    [pop] => 0
                    [sys] => stdClass Object
                        (
                            [pod] => n
                        )

                    [dt_txt] => 2022-06-03 15:00:00
                )

이 질문에 댓글 쓰기 :

답변 2


$jsondata = json_decode($response);

->

$arr = json_decode($response, true);
echo $arr['list'][0]['main']['temp'];

//print_r($jsondata);

echo $jsondata->list[0]->main->temp;

이러면 되지 않을까요?

답변을 작성하시기 전에 로그인 해주세요.
전체 251
QA 내용 검색
filter #api ×

회원로그인

(주)에스아이알소프트 / 대표:홍석명 / (06211) 서울특별시 강남구 역삼동 707-34 한신인터밸리24 서관 1404호 / E-Mail: admin@sir.kr
사업자등록번호: 217-81-36347 / 통신판매업신고번호:2014-서울강남-02098호 / 개인정보보호책임자:김민섭(minsup@sir.kr)
© SIRSOFT