Skip to content

Instantly share code, notes, and snippets.

@quangpro1610
Created June 10, 2021 09:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save quangpro1610/b17d9a1a93957ed2d9162f0f930591f1 to your computer and use it in GitHub Desktop.
Save quangpro1610/b17d9a1a93957ed2d9162f0f930591f1 to your computer and use it in GitHub Desktop.
Xử lý lấy dữ liệu thời tiết trong ngày tại địa điểm
<?php
header('Content-Type: application/json; charset=utf-8');
$api_key = 'PUT_API_KEY_HERE';
if(!empty($_GET['province'])){
$province = $_GET['province'];
}else{
$province = 'Quang Tri';
}
$url = 'http://api.openweathermap.org/data/2.5/weather?q=' . $province . ',vietnam&appid=' . $api_key . '&units=metric&lang=vi';
$content = file_get_contents($url);
$json = json_decode($content, true);
if(isset($json) && !empty($json)){
$result = array(
"messages" => array(
array(
'text' => "Bạn đã tra cứu dữ liệu thời tiết cho " . $province . ". Dưới đây là thông tin thời tiết của ngày hôm nay. Bạn nên chú ý để mang theo vật dụng tránh mưa, nắng khi đi ra ngoài.\n\n- Nhiệt độ: " .floor($json['main']['temp']) . "°C \n- Độ ẩm: " .$json['main']['humidity'] . "% \n- Tình trạng thời tiết: " . ucfirst($json['weather'][0]['description'])
)
)
);
}else{
$result = array(
"messages" => array(
array("text" => "Không tìm thấy dữ liệu của thành phố bạn tra cứu 🙁 \nVui lòng thử tìm thành phố lân cận khác.")
)
);
}
echo json_encode($result, JSON_UNESCAPED_UNICODE);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment