상세 컨텐츠

본문 제목

야휴 날씨 API 서비스변경에 따른 코드변경처리

기타정보

by 김일국 2016. 4. 19. 22:44

본문

지난 포스트에 다음 API도 OAuth2.0 으로 변경해서 수정하느라 고생했는데,

이번에 야후 날씨 정보 불러오는 것도 변경되어서 소스를 수정해야 했습니다. 이것은 수정 후기 입니다.

PHP수정의 핵심은

기존: $url = 'http://xml.weather.yahoo.com/forecastrss?w='.$code.'&u=c'; // rss 서비스

수정: $yql_query = 'select * from weather.forecast where woeid ="'.$code.'" and u="c"';  // Yahoo! Query Language 로 변경

전체 코드는 아래와 같습니다.

$BASE_URL = "http://query.yahooapis.com/v1/public/yql";
  $xml = array('Seoul' => 1132599, 'Tokyo' => 1118370, 'Beijing' => 2151330);
  foreach($xml as $city => $code) {
   //$url = 'http://xml.weather.yahoo.com/forecastrss?w='.$code.'&u=c';
   $yql_query = 'select * from weather.forecast where woeid ="'.$code.'" and u="c"';
   $url = $BASE_URL . "?q=" . urlencode($yql_query) . "&format=xml";
   $ch = @curl_init();
   $timeout = 10;
   @curl_setopt($ch, CURLOPT_URL, $url);
   @curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
   @curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
   $yahooxml = @curl_exec($ch);
   @curl_close($ch);
   if (@simplexml_load_string($yahooxml))
   {
   $smplxml = @simplexml_load_string($yahooxml);
   @$smplxml->registerXpathNamespace('yweather', 'http://xml.weather.yahoo.com/ns/rss/1.0');
   @$children = $smplxml->xpath('//channel/item/yweather:condition'); 
   }
   else
   {
   $city = "Reload!";
   $children[0]['text'] = "http://weather.yahoo.com";
   $children[0]['date'] = "Traffic jam!";
   }
  ?>
  <div class="tile-content slide">
                    <div class="padding10" style="background: url(http://us.i1.yimg.com/us.yimg.com/i/us/we/52/<?=$children[0]['code']?>.gif) no-repeat top right;">
                        <h1 class="fg-white ntm"><?=$children[0]['temp']?>°</h1>
                        <h1 class="fg-white no-margin"><?=$city?></h1>
                        <h5 class="fg-white no-margin"><?=$children[0]['text']?></h5>
                        <p class="tertiary-text fg-white no-margin">Today</p>
                        <h4 class="fg-white no-margin"><?=$children[0]['date']?></h4>
                    </div>
                </div>
  <? } ?>

수정한 후 결과화면(아래 붉은색 부분 구름있고, 영상8도 2016.4.19 일)


관련글 더보기

댓글 영역