테스트 사이트 - 개발 중인 베타 버전입니다

api 날씨 이거 코드 어떻게 만들어야하나요? 채택완료

zkdlf 8년 전 조회 3,077

api.openweathermap.org/data/2.5/forecast/city?id=524901&APPID=1111111111

 

api key 발급받아서 적용할려는데.. 어떻게 해야하는지 모르겠어요...

 

코드 좀 있으면 보내주시면 감사하겠습니다​

 

이건 예전에 사용하던건데 안되서 위에있는 날씨정보로 바꿀려고하는거라서

 

<meta http-equiv="content-type" content="text/html; charset=utf-8">

<?php 
 //*Copyright © HoneyMusic*
 //*Yu-sung heon          *
 //*density2002@naver.com *

  $url ="http://api.openweathermap.org/data/2.5/forecast/city?id=524901&APPID=2f8ba4c1223f842a81c7b3e687927703">http://api.openweathermap.org/data/2.5/forecast/city?id=524901&APPID=2f8ba4c1223f842a81c7b3e687927703"

 /* $url = "http://api.wunderground.com/api/3da28c0dbab2f4e7/geolookup/conditions/lang:EN/q/Korea/Seoul.json">http://api.wunderground.com/api/3da28c0dbab2f4e7/geolookup/conditions/lang:EN/q/Korea/Seoul.json"; */
 
  $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
    curl_setopt($ch, CURLOPT_TIMEOUT, 60);
    $return = curl_exec($ch);
    curl_close($ch);
 
 
 
  $json_string = $return;
  $parsed_json = json_decode($json_string);
  $location = $parsed_json->{'location'}->{'city'};
  $temp_f = $parsed_json->{'current_observation'}->{'temp_f'};
  $temp_c = $parsed_json->{'current_observation'}->{'temp_c'};
  $uv = $parsed_json->{'current_observation'}->{'UV'};
  $wind_kph = $parsed_json->{'current_observation'}->{'wind_kph'};
  $weather = $parsed_json->{'current_observation'}->{'weather'};
  $icon_url = $parsed_json->{'current_observation'}->{'icon_url'};
 
  $api = "현재 날씨를 알려 드리겠습니다 $location 에는 섭씨$temp_f 도씨이며 온도 $temp_c 도씨입니다.현재날씨는 $weather 입니다. 풍속은 $wind_kph 입니다. 좋은 하루되십시요\n";
  $apidata = "$location  $temp_c ℃ &nbsp; $weather  \n";
 class TextToSpeech {
 public $mp3data;
 function __construct($text="") {
        $text = trim($text);
        if(!empty($text)) {
            $text = urlencode($text);
            $url = "http://translate.google.com/translate_tts?ie=utf-8&tl=ko&q=".$text;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
    curl_setopt($ch, CURLOPT_TIMEOUT, 60);
    $return = curl_exec($ch);
    curl_close($ch);

 $this->mp3data = $return;
        }
    }
 
    function setText($text) {
        $text = trim($text);
        if(!empty($text)) {
            $text = urlencode($text);
            $url = "http://translate.google.com/translate_tts?ie=utf-8&tl=ko&q=".$text;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
    curl_setopt($ch, CURLOPT_TIMEOUT, 60);
    $return = curl_exec($ch);
    curl_close($ch);

 $this->mp3data = $return;
            return $mp3data;
        } else { return false; }
    }
 
    function saveToFile($filename) {
        $filename = trim($filename);
        if(!empty($filename)) {
            return file_put_contents($filename,$this->mp3data);
        } else { return false; }
    }
 
}

$data=$api;

$tts = new TextToSpeech();
$tts->setText($data);

?>

<img src="<?=$icon_url;?>" height="45" ><? echo $apidata;
?>
 

댓글을 작성하려면 로그인이 필요합니다.

답변 1개

채택된 답변
+20 포인트
8년 전

file_get_contents로 간편하게 가져오셔도 되구요, 사용하신 것처럼 CURL로 가져와도 됩니다.

 

그냥 해당 페이지를 긁어온 다음 json_decode로 풀어서 쓰시면 됩니다.

로그인 후 평가할 수 있습니다

답변에 대한 댓글 1개

z
zkdlf
8년 전
Parse error: syntax error, unexpected T_VARIABLE in /home/tnhands/public_html/board/ds/ds.php on line 10

에러 뜨는데 왜그런가요?

index.php
<div class="m01">
<?php
include_once("./board/ds/ds.php");
?>

ds.php

<meta http-equiv="content-type" content="text/html; charset=utf-8">
<?php

$url = "http://api.openweathermap.org/data/2.5/weather?q=Seoul&mode=json&units=metric&APPID=11111111111111111"

10번줄 $text= file_get_contents($url);
echo $text;
$temp =@explode('첫부분',$text);
$temp=@explode('끝부분',$temp[1]);
$text =$temp[0];
print_r($text);

?>

댓글을 작성하려면 로그인이 필요합니다.

답변을 작성하려면 로그인이 필요합니다.

로그인