0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【PHP】Yahoo 気象情報API から雨量情報を取得してみた

Last updated at Posted at 2021-10-26

初めに

ダッシュボード作成のために、雨量をAPIにて取得する
YOLP 気象情報API を使用する。
⇒PHPを用い、HP等で表示させる用途を想定

リファレンスより引用
指定した緯度経度の雨の強さを取得できるAPIです。
現在時刻の降水強度実測値から、60分後までの降水強度予測値を取得できます。

環境

  • Windows10
  • PHP 8.0.6

パラメータ

  • appid => Yahooデベロッパー登録をし、取得する
  • coordinates => 経度,緯度(世界測地系)
  • output => 出力形式を指定(json/xml)
  • date =>日時を指定(YYYYMMDDHHMI形式)

本コードではparamsにてパラメータをセットしてください。

コード

yahoo_weather_api.php
<?php
//Yahoo weather APIにて気象情報を取得する
$api = 'https://map.yahooapis.jp/weather/V1/place?';
//パラメータをセット
$params = array(
    "appid" => "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
    "coordinates" => "135.000000,35.0000000",
    "output" =>  "json",
    "date" => date("YmdHi", strtotime("now")),
);

$url = $api . 'appid=' . $params['appid'] . "&coordinates=" . $params["coordinates"] . "&output=" . $params["output"];
$weather_json = file_get_contents($url);
$weather_array = json_decode($weather_json, true);

//降水強度実測値を変数に格納
$date = $weather_array["Feature"]["0"]["Property"]["WeatherList"]["Weather"]["0"]["Date"];
$rainfall=$weather_array["Feature"]["0"]["Property"]["WeatherList"]["Weather"]["0"]["Rainfall"];

//出力
echo "日時:" . $date . "\n";
echo "雨量:" . $rainfall. "\n";

終わりに

実測値といえども、気象レーダー解析雨量のため実際の降雨量と差異があるが、アメダスがない地点の雨量も取得できるのは魅力的。

※降水強度は、気象レーダーで観測された降水の強さを時間雨量(mm/h)に換算した値で、実際の雨量とは異なります。

参考サイト

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?