<openweathermap api >curl_test

hsanol62·2022년 1월 21일
0

test php코드 출처: https://namjackson.tistory.com/27

api주소: https://openweathermap.org/

postman에 url만 get 한 결과

php로 'curl' test


<?
$w = curl_init('http://api.openweathermap.org/data/2.5/weather?q=Seoul&appid=(key값)');
$weather_options = array(
	CURLOPT_HEADER => false,
	CURLOPT_RETURNTRANSFER => true
	);
curl_setopt_array($w, $weather_options);
$a = curl_exec($w);
curl_close($w);

if (isset($a) && $a) {
	$weather = json_decode($a);
	
	$current_temp = $weather->main->temp - 273.15;
	$temp_min = $weather->main->temp_min - 273.15;
	$temp_max = $weather->main->temp_max - 273.15;
	$weather_main = $weather->weather[0]->main;
	$weather_icon = "http://openweathermap.org/img/w/{$weather->weather[0]->icon}.png";

	echo "
";
	echo "현재기온 : " . $current_temp;
	echo "<br>최저기온 : " . $temp_min;
	echo "<br>최고기온 : " . $temp_max;
	echo "<br>날씨 : " . $weather_main;
	echo "
";
} else {
	exit(0);
}

?>

실행결과:

profile
공부중

0개의 댓글