1
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?

【PHP】定時飛行場実況気象通報式をAPIから取得してみた

Last updated at Posted at 2025-08-18

定時飛行場実況気象通報式(METAR)とは

空港の気象状況について、航空機の離着陸のため自動で行った観測の成果(航空気象観測機器で観測された成果)を自動で通報している電文である。

空港の気象状況を取得するため、AVWX REST APIを用いて、METARを取得した。

プログラム

与論空港(RORY)の例
他の空港の場合はURLの最後を書き換えること

get_metar.php
<?php
// 与論空港(RORY)のMETARデータを取得するプログラム

$ch = curl_init();

// APIエンドポイントの設定(JSONフォーマットで取得)
curl_setopt($ch, CURLOPT_URL, "https://avwx.rest/api/metar/RORY?options=&airport=true&reporting=true&format=json");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);

// 必要に応じてAPIトークンを設定
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
	"Authorization: Token [your API token]"
));

$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

// エラーハンドリング
if ($response === FALSE) {
	echo "cURLエラーが発生しました。\n";
	exit(1);
}

if ($http_code !== 200) {
	echo "HTTPエラー: " . $http_code . "\n";
	echo "レスポンス: " . $response . "\n";
	exit(1);
}

// JSONデータをデコード
$metar_data = json_decode($response, true);

if (json_last_error() !== JSON_ERROR_NONE) {
	echo "JSONデコードエラー: " . json_last_error_msg() . "\n";
	echo "生レスポンス: " . $response . "\n";
	exit(1);
}

echo "\n=== 完全なJSONデータ ===\n";
echo json_encode($metar_data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);

レスポンス

与論空港の例

response.json
{
    "altimeter": {
        "repr": "Q1014",
        "spoken": "one zero one four",
        "value": 1014
    },
    "clouds": [
        {
            "altitude": 90,
            "modifier": null,
            "repr": "FEW090",
            "type": "FEW"
        },
        {
            "altitude": 130,
            "modifier": null,
            "repr": "SCT130",
            "type": "SCT"
        },
        {
            "altitude": 150,
            "modifier": null,
            "repr": "BKN150",
            "type": "BKN"
        }
    ],
    "density_altitude": 1601,
    "dewpoint": {
        "repr": "25",
        "spoken": "two five",
        "value": 25
    },
    "flight_rules": "VFR",
    "meta": {
        "cache-timestamp": "2025-08-18T06:22:02.030000Z",
        "stations_updated": "2024-12-02",
        "timestamp": "2025-08-18T06:22:59.583557Z"
    },
    "other": [
        "TCU"
    ],
    "pressure_altitude": 29,
    "raw": "RORY 180600Z AUTO 14007KT 9999 FEW090 SCT130 BKN150 \/\/\/\/\/\/TCU 28\/25 Q1014",
    "relative_humidity": 0.8377219417290783,
    "remarks": "",
    "remarks_info": null,
    "runway_visibility": [],
    "sanitized": "RORY 180600Z 14007KT 9999 FEW090 SCT130 BKN150 TCU 28\/25 Q1014",
    "station": "RORY",
    "temperature": {
        "repr": "28",
        "spoken": "two eight",
        "value": 28
    },
    "time": {
        "dt": "2025-08-18T06:00:00Z",
        "repr": "180600Z"
    },
    "units": {
        "accumulation": "in",
        "altimeter": "hPa",
        "altitude": "ft",
        "temperature": "C",
        "visibility": "m",
        "wind_speed": "kt"
    },
    "visibility": {
        "repr": "9999",
        "spoken": "nine nine nine nine",
        "value": 9999
    },
    "wind_direction": {
        "repr": "140",
        "spoken": "one four zero",
        "value": 140
    },
    "wind_gust": null,
    "wind_speed": {
        "repr": "07",
        "spoken": "seven",
        "value": 7
    },
    "wind_variable_direction": [],
    "wx_codes": []
}%                                       

参考文献

1
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
1
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?