Putting It to Use

   

This next script shows how to open a file from the Internet and display it on your page, as in Figure 6-1. The National Oceanic & Atmospheric Administration (NOAA) provides hourly weather information for almost every airport in the United States. They provide this information on their site at www.noaa.gov in many formats. One weather report format they provide is called METAR. METAR is a French acronym that roughly translates as "Aviation Routine Weather Report." METAR provides useful information like current winds, barometric pressure, cloud cover, etc. This script accesses the files stored on the NOAA's datastore for this information, relative to the airport identifier you provide to the script. If you enter "KJFK" for New York's JFK airport, it provides the current weather in the encoded (shorthand for aviation weather reporting) and decoded (readable by us normal people) formats.

Script 6 1 metar.php

[View full width]

  1.  <body bgcolor="#FFFFFF" text="#000000">  2.  <h2>METAR Weather Retrieval</h2>  3.  <p>This page retrieves the latest METAR information for the requested station.  4.  <p>METAR stations are generally located at all major airports and many smaller ones.  graphics/ccc.gifStation identifiers are generally the letter "K" followed by the airport identifier. For  graphics/ccc.gifexample, the station identifier for Logan International at Boston, MA is "KBOS".  5.  <p>The information is retrieved directly from the NOAA Weather Database (see <i>http:// graphics/ccc.gifweather.noaa.gov/weather/metar.shtml</i>).  6.  <p>Various METAR-related information can be found at <a href="http://205.156.54.206/oso/ graphics/ccc.gifoso1/oso12/metar.htm">http://205.156.54.206/oso/oso1/oso12/metar.htm</a>.  7.  <p><strong>Enter the Station Identifier below:</strong>  8.  <p>  9.  <form action=metar.php mehod=get> 10.  <p>METAR Station ID: <input type="text" name="metar_station" size="4" maxlength="4"> 11.  <input type="submit" name="submit" value="Submit"> 12.  </form> 13.  <hr noshade> 14.  <? 15.  function connect_error($metar_station) { 16.      ?> 17.      <p><span class=navy>Sorry, either that station identifier, <strong><? echo strtoupper( graphics/ccc.gif$metar_station) ?></strong>, is not valid, or the connection to the NOAA servers is  graphics/ccc.gifbroken.</span> 18.    <? 19.    die(); 20.  } 21.  if(isset($metar_station)) { 22.  $file1 = "ftp://weather.noaa.gov/data/observations/metar/decoded/" . strtoupper( graphics/ccc.gif$metar_station) . ".TXT"; 23.  $file2 = "ftp://weather.noaa.gov/data/observations/metar/stations/" . strtoupper( graphics/ccc.gif$metar_station) . ".TXT"; 24.  if(!$fd1 = @fopen($file1, "rb")) { 25.    connect_error($metar_station); 26.  } else { 27.    $decoded = fread($fd1, 9999999); 28.    printf("<h3>METAR Decoded Observation for <span class=navy>%s</span></h3><pre>%s</ graphics/ccc.gifpre>", strtoupper($metar_station), htmlentities($decoded)); 29.    fclose($fd1); 30.  } 31.  if(!$fd2 = @fopen($file2, "rb")) { 32.    connect_error($metar_station); 33.  } else { 34.    $encoded = fread($fd2, 9999999); 35.    printf("<h3>METAR Encoded Observation for <span class=navy>%s</span></h3><pre>%s</ graphics/ccc.gifpre>", strtoupper($metar_station), htmlentities($encoded)); 36.    fclose($fd2); 37.  } 38.  } 39.  ?> 40.  </body> 41.  </html> 
Figure 6-1. metar.php

graphics/06fig01.jpg

Script 6-1. metar.php Line-by-Line Explanation

LINE

DESCRIPTION

1 7

Provide some basic HTML for the beginning of the page, with a style sheet and some instructions on how to use the script.

9 13

Provide a form asking for the airport station identifier.

14

Begin the PHP part of the page.

15 20

Define a function, connect_error(), to print out an error message in the event that the connection to the NOAA server is not working or an invalid station identifier was entered by the user. This function takes one argument, $metar_station, which is the station identifier entered by the user into the form.

21

If the $metar_station variable is set, then we know that the user has submitted the form, and can continue to the next line. If $metar_station has not been set, then do nothing.

22

Assign the decoded METAR file from the NOAA server to the $file1 variable. The files reside on the server with the format METAR_STATION.TXT for example, "KBOS.TXT" for Logan airport in Boston, MA. The strtoupper() function makes the user's text all uppercase regardless of what case it was when the user entered it.

23

The same as above, only this time open the encoded version of the data and assign it to the $file2 variable.

24 25

Attempt to open the file on the NOAA server. If the file cannot be opened, then throw an error to the connect_error() function to notify the user.

26

If there were no errors opening the file, then execute lines 27 30.

27

Read in the entire contents of the file (we are assuming it is less than 9,999,999 bytes) and assign it to the $decoded variable.

28

Print out the data to the page. We use htmlentities() to translate any data that may be recognized as HTML code into the HTML code for that data.

29

Close the file using the fclose() function.

30

Close out the if statement started on line 26.

31 32

Attempt to open the other file on the NOAA server. If the file cannot be opened, then throw an error to the connect_error() function to notify the user.

33

If there were no errors opening the file, then execute lines 34 37.

34

Read in the entire contents of the file (we are assuming it is less than 9,999,999 bytes) and assign it to the $decoded variable.

35

Print out the data to the page. We use htmlentities() to translate any data that may be recognized as HTML code into the HTML code for that data.

36

Close the file using the fclose() function.

37

Close out the if statement started on line 33.

38

Close out the if statement started on line 31.

39 41

End the PHP and HTML for the script.


   
Top


Advanced PHP for Web Professionals
Advanced PHP for Web Professionals
ISBN: 0130085391
EAN: 2147483647
Year: 2005
Pages: 92

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net