アメダスデータ取得
気象庁データをjsonで直によむ
http://www.jma.go.jp/bosai/amedas/data/point/32287/20210407_18.json
32287が観測所番号。
日付の後ろは3時間きざみ
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>アメPHP</title>
</head>
<body>
<?php
$pointNo="32287";
$today = date("Ymd"); //今日8桁
$startHour = date("h");
$startHour -= $startHour % 3 ;
$startHour = sprintf('%02d', $startHour); // 0梅 開始時間3時間毎2桁
$startTime = sprintf('%08d%02d0000', $today,$startHour);
$nowHour =date("h");
$nowMinit =date("i");
$nowMinit -= $nowMinit % 10;
//$nowMinit = sprintf('%02d', $nowMinit); //2桁
$endTime =sprintf('%08d%02d%02d00',$today,$nowHour,$nowMinit);
$url = "http://www.jma.go.jp/bosai/amedas/data/point/" . $pointNo ."/".$today . "_". $startHour .".json";
echo $url;echo "<br>";
$json = file_get_contents($url);
$json = mb_convert_encoding($json, 'UTF8', 'ASCII,JIS,UTF-8,EUC-JP,SJIS-WIN');
$arr = json_decode($json,true);
if ($arr === NULL) {
echo "no data";
return;//〜データがない時の処理〜
}else{
echo "<table><tr>
<th>時刻</th><th>気温</th><th>風向</th><th>風速</th><th>降水量10m</th><th>降水量1h</th><th>降水量24h</th><th>最低気温</th><th>最高気温</th><th>最大風速</th>
</tr>
";
for($i=$startTime ;$i <=$endTime ;$i +=1000){
if( substr($i,10,1) == 6){$i += 4000;}
echo"<tr>";
echo "<td>".$i ."</td>";
if($arr[$i]['temp'][0]){
echo "<td>".$arr[$i]['temp'][0]."</td>";
echo "<td>".$arr[$i]['windDirection'][0]."</td>";
echo "<td>".$arr[$i]['wind'][0]."</td>";
echo "<td>".$arr[$i]['precipitation10m'][0]."</td>";
echo "<td>".$arr[$i]['precipitation1h'][0]."</td>";
echo "<td>".$arr[$i]['precipitation24h'][0]."</td>";
$getTimeH =$arr[$i]['minTempTime']['hour'];
$getTimeH += 9;
if($getTimeH >= 24){$getTimeH -= 24;}
$getTimeM =$arr[$i]['minTempTime']['minute'];
$printStr = sprintf('%02d:%02d', $getTimeH,$getTimeM);
$getData =$arr[$i]['minTemp'][0];
echo "<td>".$printStr ."__". $getData."</td>";
$getTimeH =$arr[$i]['maxTempTime']['hour'];
$getTimeH += 9;
if($getTimeH >= 24){$getTimeH -= 24;}
$getTimeM =$arr[$i]['maxTempTime']['minute'];
$printStr = sprintf('%02d:%02d', $getTimeH,$getTimeM);
$getData =$arr[$i]['maxTemp'][0];
echo "<td>".$printStr ."__". $getData."</td>";
$getTimeH =$arr[$i]['gustTime']['hour'];
$getTimeH += 9;
if($getTimeH >= 24){$getTimeH -= 24;}
$getTimeM =$arr[$i]['gustTime']['minute'];
$printStr = sprintf('%02d:%02d', $getTimeH,$getTimeM);
$getData =$arr[$i]['gustDirection'][0];
$getData2 =$arr[$i]['gust'][0];
echo "<td>".$printStr ."__". $getData."__". $getData2."</td>";
}else{
echo "<td>no Data</td>";
}
echo "</tr>\n";
}
echo "</table>";
}
?>
</body>
</html>
1 2