To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
PHPBuilder.com  
 

 

Go Back   PHPBuilder.com > PHP Help > Upgrading PHP

Upgrading PHP Issues concerning PHP version upgrades and future releases

Reply
 
Thread Tools Rate Thread Display Modes
Old 08-14-2004, 10:54 AM   #1
yelvington
Senior Member
 
Join Date: Dec 2000
Location: USA
Posts: 1,012
How to get current weather with PHP5

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:

http://www.nws.noaa.gov/data/current_obs/

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.
PHP Code:
<?
$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:

PHP Code:
<?
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.
yelvington is offline   Reply With Quote
Old 08-28-2004, 08:04 AM   #2
kkobashi
Senior Member
 
kkobashi's Avatar
 
Join Date: Aug 2004
Location: Silicon Valley, CA
Posts: 273
Very cool dude
__________________
Kerry Kobashi
Kobashi Computing
kkobashi is offline   Reply With Quote
Old 09-14-2004, 12:27 PM   #3
Stinger51
Senior Member
 
Join Date: Aug 2002
Location: Florida Sun Coast
Posts: 200
Will this work with PHP4?

Will 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.
__________________
If everybody's thinking alike, somebody isn't thinking.
Stinger51 is offline   Reply With Quote
Old 09-16-2004, 07:09 AM   #4
Weedpacket
Custom User Title™
 
Weedpacket's Avatar
 
Join Date: Aug 2002
Location: Rapid Offensive Unit "Foreign Object Damage"
Posts: 19,137
Re: Will this work with PHP4?

Quote:
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, and also a thread in this forum).
__________________
On two occasions I have been asked [by Members of Parliament], "Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?" I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question.

Last edited by Weedpacket; 09-16-2004 at 07:33 AM.
Weedpacket is offline   Reply With Quote
Old 07-12-2006, 09:38 AM   #5
joad
Member
 
Join Date: Jan 2006
Posts: 50
Quote:
Originally Posted by yelvington
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:

http://www.nws.noaa.gov/data/current_obs/
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.

Last edited by joad; 07-12-2006 at 10:01 AM.
joad is offline   Reply With Quote
Old 07-15-2006, 03:21 AM   #6
Kudose
Fatal Error Line 0
 
Kudose's Avatar
 
Join Date: Feb 2003
Location: Kansas City
Posts: 3,818
Quote:
Originally Posted by Stinger51
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.
__________________
Problem Solved? Mark this thread resolved using the Thread Tools.
Apache HQ | PHP HQ | Smarty HQ | MySQL HQ | Spechal Blog | LinkedIn
0100001001111001011101000110010100100000011011010110010100100001
Kudose is offline   Reply With Quote
Old 07-15-2006, 03:22 AM   #7
Kudose
Fatal Error Line 0
 
Kudose's Avatar
 
Join Date: Feb 2003
Location: Kansas City
Posts: 3,818
Oh Snap!

Quote:
Originally Posted by joad
I've been trying to do this for ages - has anyone come across a similar feed for London please ?
http://weather.nearperfect.com/weather/
__________________
Problem Solved? Mark this thread resolved using the Thread Tools.
Apache HQ | PHP HQ | Smarty HQ | MySQL HQ | Spechal Blog | LinkedIn
0100001001111001011101000110010100100000011011010110010100100001
Kudose is offline   Reply With Quote
Old 07-15-2006, 04:39 AM   #8
joad
Member
 
Join Date: Jan 2006
Posts: 50
Question

Many thanks Kudose - it's appreciated.

The one for London on that link is :

http://weather.nearperfect.com/weath...rss/010/c00032

I put that in :

http://www.130605.com/weather/

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.

Last edited by joad; 07-15-2006 at 08:50 AM.
joad is offline   Reply With Quote
Old 07-15-2006, 10:54 PM   #9
Kudose
Fatal Error Line 0
 
Kudose's Avatar
 
Join Date: Feb 2003
Location: Kansas City
Posts: 3,818
Post up the code you used to implement it.
__________________
Problem Solved? Mark this thread resolved using the Thread Tools.
Apache HQ | PHP HQ | Smarty HQ | MySQL HQ | Spechal Blog | LinkedIn
0100001001111001011101000110010100100000011011010110010100100001
Kudose is offline   Reply With Quote
Old 07-17-2006, 06:05 AM   #10
joad
Member
 
Join Date: Jan 2006
Posts: 50
Question

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.130605.com/styles.css">
</head>
<body>
<table width="100%">
<tr>
<td>&nbsp;</td>
<td><br>
<br>
&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>http://weather.nearperfect.com/weather/world.rss/010/c00032</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
</table>
</body>
</html>
joad is offline   Reply With Quote
Old 07-17-2006, 11:36 PM   #11
Kudose
Fatal Error Line 0
 
Kudose's Avatar
 
Join Date: Feb 2003
Location: Kansas City
Posts: 3,818
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?
__________________
Problem Solved? Mark this thread resolved using the Thread Tools.
Apache HQ | PHP HQ | Smarty HQ | MySQL HQ | Spechal Blog | LinkedIn
0100001001111001011101000110010100100000011011010110010100100001
Kudose is offline   Reply With Quote
Old 07-18-2006, 03:46 AM   #12
joad
Member
 
Join Date: Jan 2006
Posts: 50
Question

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.
joad is offline   Reply With Quote
Old 07-18-2006, 04:31 AM   #13
Kudose
Fatal Error Line 0
 
Kudose's Avatar
 
Join Date: Feb 2003
Location: Kansas City
Posts: 3,818
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.
__________________
Problem Solved? Mark this thread resolved using the Thread Tools.
Apache HQ | PHP HQ | Smarty HQ | MySQL HQ | Spechal Blog | LinkedIn
0100001001111001011101000110010100100000011011010110010100100001
Kudose is offline   Reply With Quote
Old 07-18-2006, 04:43 AM   #14
joad
Member
 
Join Date: Jan 2006
Posts: 50
Quote:
Originally Posted by Kudose
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 :

http://www.130605.com/weather/

<!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.130605.com/styles.css">
</head>
<body>
<table width="100%">
<tr>
<td>&nbsp;</td>
<td><br>
<br>
&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</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>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
</table>
</body>
</html>


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

Any help appreciated.

Joad.
joad is offline   Reply With Quote
Old 07-18-2006, 04:54 AM   #15
Kudose
Fatal Error Line 0
 
Kudose's Avatar
 
Join Date: Feb 2003
Location: Kansas City
Posts: 3,818
I got it to work with just this in a file:
PHP Code:
<?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 />';
?>
__________________
Problem Solved? Mark this thread resolved using the Thread Tools.
Apache HQ | PHP HQ | Smarty HQ | MySQL HQ | Spechal Blog | LinkedIn
0100001001111001011101000110010100100000011011010110010100100001
Kudose is offline   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT -4. The time now is 05:29 PM.






Acceptable Use Policy

internet.comMediabistrojusttechjobs.comGraphics.com

WebMediaBrands Corporate Info


Advertise | Newsletters | Feedback | Submit News

Legal Notices | Licensing | Permissions | Privacy Policy


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.