I have a bunch of aviation sites, many that require weather information. I like to include raw and unformatted TAF and METAR information on a number of locations. This weather information is presented as a string of text that can be quickly deciphered by pilots.
This is a small snippet of code that will retrieve METAR information from the noaa.gov website and make it available in a way that can be easily formatted. A list of the available stations is available here.
By using the code below it will retrieve information from the noaa website on every request. For that reason, I request data for numerous locations every three hours and store data in a cache file – thus negating the need to make repeated requests to noaa.gov; and it speeds up the delivery time considerably.
<?php
$location = "YSSY";
get_metar($location);
function get_metar($location) {
$fileName = "http://weather.noaa.gov/pub/data/observations/metar/stations/$location.TXT";
$metar = '';
$fileData = @file($fileName) or die('METAR not available');
if ($fileData != false) {
list($i, $date) = each($fileData);
$utc = strtotime(trim($date));
$time = date("D, F jS Y g:i A",$utc);
while (list($i, $line) = each($fileData)) {
$metar .= ' ' . trim($line);
}
$metar = trim(str_replace(' ', ' ', $metar));
}
echo "METAR FOR $location (Issued: $time UTC):<br>$metar";
}
?>
Step by step
First, we open with a PHP tag, define the location we want to search and define the function that will output our metar. Location should be in UPPER CASE.
<?php $location = "YSSY"; get_metar($location);
Next, we’ll define the filename of the URL that contains our text data and define $metar as ‘empty’.
function get_metar($location) {
$fileName = "http://weather.noaa.gov/pub/data/observations/metar/stations/$location.TXT";
$metar = '';
$filedata is the contents of the noaa page. If it’s unavailable we’ll output an error. Replace the error message with anything of your choosing. PHP file function will read the content of the file into an array.
$fileData = @file($fileName) or die('METAR not available');
So, if $filedata is not empty…
if ($fileData != false) {
We’ll assign two variables – the time (line 1) and the metar (line 2) using list as if they were an array. The PHP each function will read each line of the file seperately and they’ll be assigned as variable $i and $date. The PHP ‘function’ list is not really a function, but more a construct. It will assign values starting with the right-most parameter.
list($i, $date) = each($fileData);
The strtotime function will take the timestamp from the noaa output and create a UNIX timestamp. We can later manipulate this time in any way we choose. In my case, I’ve created a long readable time but if you were truly making this information to an aviation community you wouldn’t bother including it at all since the issue time is included within the actual metar.
$utc = strtotime(trim($date));
$time = date("D, F jS Y g:i A",$utc);
Next, using a while loop, we’ll create the metar variable information from the data stored in $fileData.
while (list($i, $line) = each($fileData)) {
$metar .= ' ' . trim($line);
}
This next line is used to replace double white spaces with a single white space.
$metar = trim(str_replace(' ', ' ', $metar));
}
Next, print the metar. In this example I’ve included the long timestamp but you will probably want to omiss this and simple output the raw metar data instead.
echo "METAR FOR $location (Issued: $time UTC):<br>$metar"; } ?>
|
|
Download: Basic weather metar |
If you liked this article, you may also like:
- [Shortcode] Easily post an aviation Metar report into your post or page with shortcode
- [Shortcode] Easily post an aviation TAF report into your WordPress post or page with shortcode
- Metar to Twitter Application
- Cache Your Website with PHP (in 1 minute)
- Print all files from and below a directory with PHP


Hi,
Can you explain how to decode the results?
Did you want the results in plain text? Can you provide an example?
After I initially left a comment I seem to have clicked on the -Notify me when new comments are added- checkbox
and from now on each time a comment is added I receive four emails with the exact same comment.
Is there a means you are able to remove me from
that service? Kudos!
Hi,
When I write this code, $metar”; } ?> is the only thing that appears on my website… Since I’m French and tried with a French airport I supposed that it explained the issue, but it basically doesn’t work either with an American airport…
Gabriel.
Is there a API that not just download METARs and TAFs and how about PIREPs and maps?