PHP Weather Home Page

Using PHP Weather

Using PHP Weather is very simple. First you have to include the file phpweather.inc in your page. Then you call the function get_metar() with the four-character station-identifier. This gives you the METAR, which you can then feed to process_metar(). This function return an array that contains the various parts of the METAR in decoded form. They are also returned in both imperial (feet, miles, degrees of Fahrenheit, etc.) and metric SI units (meters, kilometers and degrees Celsius).

This code is all that is necessary to make PHP Weather work:

<?php
include('phpweather.inc');
include(
'locale_en.inc');
$metar  get_metar('EKYT');
$data   process_metar($metar);
$temp_c $data['temp_c'];
$temp_f $data['temp_f'];
echo 
"<p>The temperature is $temp_c degrees Celsius ($temp_f degrees Fahrenheit).</p>";
?>

That's it! The above code will tell you what the temperature is in Aalborg, Denmark:

The temperature is degrees Celsius ( degrees Fahrenheit).

But you'll probably want the nice large examples you saw on the previous pages? To make the examples above I've made a function called pretty_print_metar(). You use it like this:

<?php
$metar 
get_metar('EKYT');
include(
'locale_en.inc');
pretty_print_metar($metar'Aalborg, Denmark');
?>

This will give you an English text with the current weather in Aalborg, Denmark:

Sorry! There is no data available for Aalborg, Denmark.


PHP Weather Logo SourceForge Logo