How to get current weather with PHP5

liunx

Guest
I continue to be gassed by how easy it is to manipulate XML with PHP5. Here's an example of how to get current weather data onto your page using an XML feed from the U.S. National Weather Service.

First, go to NOAA and find the feed closest to you. Start here:

<!-- m --><a class="postlink" href="http://www.nws.noaa.gov/data/current_obs/">http://www.nws.noaa.gov/data/current_obs/</a><!-- m -->

Get the URL of the XML feed, not the RSS feed. The XML feed provides finegrained information.

Here I grab the data for Daniel Field, an airport in Augusta, Ga.

<?
$url = 'http://www.nws.noaa.gov/data/current_obs/KDNL.xml';
$xml = simplexml_load_file($url);
?>


You can read the XML itself to figure out the potential names of items of interest, or you can print_r() the $xml variable. Either way, it's not hard to figure out the name of what you want to display. In my case, all I wanted was a brief statement of current conditions, the temperature (F and C), and the humidity. There's a lot of other information in the file, as well as alternative presentations of the basic data.

Then do something like this:


<?
echo '<b>Augusta weather: </b><br />';
echo $xml->weather, '<br />';
echo $xml->temperature_string, '<br />';
echo $xml->relative_humidity, '% humidity <br />';
?>


It's that simple.

Now, every time your page is displayed, it has to hit the NOAA server to get the latest information. You may want to retrieve the XML file, cache it, and have simplexml_load_file() refer to the cached version instead of directly hitting the URL as I did in this example.Very cool dudeWill this XML weather scheme work with PHP 4.3?

I'm don't know what PHP5 is doing these days and was just wondering. Most commercial servers don't seem to support PHP5 as yet.

Also, is there a good link out there that describes the new features of PHP5?

Thanks in advance.Originally posted by Stinger51
Also, is there a good link out there that describes the new features of PHP5? Yes. Have you tried looking for it? (Hint: it can be found on this page (<!-- m --><a class="postlink" href="http://www.php.net/">http://www.php.net/</a><!-- m -->), and also a thread (<!-- m --><a class="postlink" href="http://www.phpbuilder.com/board/showthread.php?s=&threadid=10284286">http://www.phpbuilder.com/board/showthr ... d=10284286</a><!-- m -->) in this forum (<!-- m --><a class="postlink" href="http://www.phpbuilder.com/board/forumdisplay.php?s=&forumid=25">http://www.phpbuilder.com/board/forumdi ... forumid=25</a><!-- m -->)).I continue to be gassed by how easy it is to manipulate XML with PHP5. Here's an example of how to get current weather data onto your page using an XML feed from the U.S. National Weather Service.

First, go to NOAA and find the feed closest to you. Start here:

<!-- m --><a class="postlink" href="http://www.nws.noaa.gov/data/current_obs/">http://www.nws.noaa.gov/data/current_obs/</a><!-- m -->


I've been trying to do this for ages - has anyone come across a similar feed for London please ?

I'm on PHP version 4.4.2

Any help appreciated.

Joad.Will this XML weather scheme work with PHP 4.3?

No. simplexml_load_file was not added until PHP5.

Review the user contributed posts on the PHP.net site to view alternatives to simplexml_load_file for earlier versions of PHP.I've been trying to do this for ages - has anyone come across a similar feed for London please ?

<!-- m --><a class="postlink" href="http://weather.nearperfect.com/weather/Many">http://weather.nearperfect.com/weather/Many</a><!-- m --> thanks Kudose - it's appreciated.

The one for London on that link is :

<!-- m --><a class="postlink" href="http://weather.nearperfect.com/weather/world.rss/010/c00032">http://weather.nearperfect.com/weather/ ... 010/c00032</a><!-- m -->

I put that in :

<!-- m --><a class="postlink" href="http://www.130605.com/weather/">http://www.130605.com/weather/</a><!-- m -->

But, it didn't seem to work ?

I probably haven't put the code in the right place - if so, could someone please let me know if it goes on the same page, or maybe I have to put it onto another page that is linked to the page above ?

Any help much appreciated.

Joad.Post up the code you used to implement it.Any help appreciated.

Joad.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta name="description" content="The weather">
<link rel="stylesheet" type="text/css" href=http://www.phpbuilder.com/board/archive/index.php/"http://www.130605.com/styles.css">
</head>
<body>
<table width="100%">
<tr>
<td> </td>
<td><br>
<br>
</td>
<td> </td>
</tr>
<tr>
<td> </td>
<td>http://weather.nearperfect.com/weather/world.rss/010/c00032</td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
</tr>
</table>
</body>
</html>Well ... That's not going to do anything but display the URL.

Have you constructed any code using the method used in the first post?Many thanks for staying with this one Kudose. I did see the sections from the 1st thread, but not sure which parts go in which places ;-(

Any help appreciated.

Joad.You could simply replace the URL in your code with both sections of code from the 1st post (replacing the URL with yours) and give it a try. It should work and be a good start.You could simply replace the URL in your code with both sections of code from the 1st post (replacing the URL with yours) and give it a try. It should work and be a good start.

Many thanks again Kudose.

Have done what you kindly mentioned and now have the code below, on this site :

<!-- m --><a class="postlink" href="http://www.130605.com/weather/">http://www.130605.com/weather/</a><!-- m -->

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta name="description" content="The weather">
<link rel="stylesheet" type="text/css" href=http://www.phpbuilder.com/board/archive/index.php/"http://www.130605.com/styles.css">
</head>
<body>
<table width="100%">
<tr>
<td> </td>
<td><br>
<br>
</td>
<td> </td>
</tr>
<tr>
<td> </td>
<td>
<?
$url = 'http://www.130605.com/weather/index.php';
$xml = simplexml_load_file($url);
?>
<?
echo '<b>Augusta weather: </b><br />';
echo $xml->weather, '<br />';
echo $xml->temperature_string, '<br />';
echo $xml->relative_humidity, '% humidity <br />';
?>
</td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
</tr>
</table>
</body>
</html>


It isn't showing quite right, so I may have misunderstood somewhere ?

Any help appreciated.

Joad.I got it to work with just this in a file:
<?php
$url = 'http://www.nws.noaa.gov/data/current_obs/KMCI.xml';
$xml = simplexml_load_file($url);
echo '<b>Kansas City weather: </b><br />';
echo $xml->weather, '<br />';
echo $xml->temperature_string, '<br />';
echo $xml->relative_humidity, '% humidity <br />';
?>Are you sure your server is running PHP5?Are you sure your server is running PHP5?

Hi Kudose - as mentioned earlier, I'm on PHP version 4.4.2

Any help appreciated.

Joad.Oops. I missed that. Let me give it another shot. I will get back to you.Oops. I missed that. Let me give it another shot. I will get back to you.


Many thanks Kudose - that would be very much appreciated.

Joad.Looks like you will need to use DOM functions which I am not really familiar with.

Sorry.

<!-- m --><a class="postlink" href="http://us3.php.net/manual/en/ref.domxml.phpOh">http://us3.php.net/manual/en/ref.domxml.phpOh</a><!-- m --> well, thanks for trying Kudose. Anyone else here know a way to do it please ?

Any help appreciated.

Joad.
 
Back
Top