53x11.com


The latest 53x11 album, Rouleur is now available for purchase and Mp3 download.


Weather.com XML Parser, 1.4

Friday, April 15th, 2005 @ 10:48 PM - code

» Download Weather.com XML Parser, 1.4 (.zip 120kb)

Latest Version Released on December 24th, 2012

Licensed under BSD, Copyright © 2014

Weather.com XML Parser Example

This PHP script will allow you to monitor your current local weather via a Weather.com XML feed. It parses the XML data and then outputs formatted HTML to a .txt file. This .txt file can then be integrated into your website via SSI, PHP or any other language that will accept Includes. It is intended to be setup as a Cron Job, that is accessed to update only every few minutes. If it were accessed more often, Weather.com would shut down your XML feed.

  1. You need to acquire a Partner ID and License Key from Weather.com. This service is completely free, and they only want your email. Visit this URL to sign up.

  2. Once you have your info from Weather.com, you will need to open weather.php with a text editor and configure the script.

    Input the $zipcode you wish to track the current conditions for. Enter your Partner ID and License Key. Input the root path to the weather.txt file. This path is critical because it will need to be accessed via PHP on your server with Cron Job.

    Finally, enter the path to the Weather icon folder. This is the path your (SSI, PHP, etc) file with the Include will use for finding the icons. For example, if you are accessing the weather information from your index.php file, and the weather folder was located in images/weather - then the path would be "images/weather".

    If you would like the results of the script emailed to you each time it runs, set $email to TRUE and input your email address.

    To use Metric units (Celsius and KMH), set $units to metric.

  3. If you have a working knowledge of PHP and HTML, you can customize what will be output to the weather.txt file. Edit this information in the area listed.

  4. Upload weather.php and the weather folder to your website.

  5. CHMOD weather.txt to 744

  6. CHMOD weather.php to 744

  7. Now you need to setup a Cron Job to access your server's php and then the weather.php. The following Cron Job is a sample and will update your weather.txt file every 10 minutes.

    */10 * * * * /usr/local/bin/php public_html/weather.php
    

    You will need to alter the PHP location and root location of your weather.php to match your server.

  8. Now weather.txt file will be updated with the current conditions at the intervals you designated in your Cron Job. You just need to use an Include function to add the content in your webpage. For example:

    <?php include("weather.txt"); ?>
    

    Or

    <!--#include file="weather.txt" -->
    

That's it, you are now a bona-fide meteorologist - don't fuck up!

Article was last edited on Wednesday, August 6th, 2008 @ 2:53 PM

Version History

1.4 (05/06/21) - 24h clock option

1.3 (05/06/18) - Metric unit support

1.0 (05/05/15) - Weather.com XML Parser

1.1 (05/04/24) - Added email alert

Comments

Tommy says
Saturday, June 18th, 2005 @ 1:56 PM

Nice piece of job!

But how do I convert into celsius, 24h and meter per second?

Nick says
Sunday, June 19th, 2005 @ 12:53 PM

I just added the option of using Metric units.

Tommy says
Sunday, June 19th, 2005 @ 1:11 PM

Thanks!

Tom says
Wednesday, June 22nd, 2005 @ 12:18 PM

I'm having a few problems getting this installed. Question, I haven't set the cron or CHMOD is this why I'm getting a parsing error? We currently have a virtual hosting solution and I don't have root level access to the server to modify cron and CHMOD. Is there a work-around? Thanks!

Dan says
Wednesday, June 22nd, 2005 @ 10:12 PM

I think this is a great script! Thanks for making it available.

What about an option to show when the Weather XML feed was last updated, rather than when the script was last run on the server?

Victoria says
Thursday, June 23rd, 2005 @ 10:44 AM

This script is awesome!! Thanks for sharing. One thing -- I'm interested whether it could use weather info from sites that don't have all the restrictions that weather.com does -- info from http://weather.noaa.gov is in public domain. (For my non-profit organization, I'd have to follow all the rules).

Marcel says
Monday, July 4th, 2005 @ 1:16 PM

Fine script! Thanks. For those of you who want the weather for non-US locations (e.g. Havana, Cuba), use this URL to find the locID to be used as $zipcode: http://xoap.weather.com/search/search?where=havana

Sammy says
Wednesday, August 24th, 2005 @ 11:27 PM

Beautiful script. Thanks for sharing. Exactly what I needed for one of my sites. Had a bit of trouble with the Cron Job but once that was sorted out I was golden. Again Thanks.

Bob says
Wednesday, September 21st, 2005 @ 11:24 AM

nice, very nice. i have spent 4 days doing what you have done. except mine was growing and growing. i saw yours pulled the guts out of it, and it does what i wanted to do all along. the secrte to yours is the foreach ($vals as $key => $i) { bit. very neat.

mucho kudos to you.

A NOTE TO GUEST ABOVE. i dont think a 5 day forecast will fit in the tiny table easily. but if you download the sdk from weather.com they provide a php script that will return a 5 day forecast.

best wishes bob.

Ya§een says
Sunday, November 20th, 2005 @ 7:03 AM

Thanx for the script .. I like your Website, the most original and crazy I never see ;-))

Chad R. Smith says
Thursday, August 28th, 2008 @ 3:51 PM

Yes, the script as is is broken.

The server changed, and a variety of other things did too.

  1. Like 42 where it says $file put:

    $file = "<a rel="nofollow" href="http://xoap.weather.com/weather/local/">http://xoap.weather.com/weather/local/</a>" . $zipcode . "?cc=*&dayf=1&link=xoap&prod=xoap&par=" . $partner . "&key=" . $license;
    
  2. In the foreach of $vals put:

    foreach ($vals as $key => $i) 
    {
        if ($vals[$key]['value'] != false) 
       {
            if($vals[$key]['tag'] == "TMP")
           {
                // This is the temperature
               $temp = $vals[$key]['value'];
            }
    
            if($vals[$key]['tag'] == "T")
            {
                // This is the wind direction
                $wind = $vals[$key]['value'];
            }
    
            if($vals[$key]['tag'] == "ICON")
            {
                // This is the number of icon to display
                $theIcon = $vals[$key]['value'];
            }
    
            if($vals[$key]['tag'] == "S")
            {
                // This is the wind speed
                $theSpeed = $vals[$key]['value'];
            }
    
            if($vals[$key]['tag'] == "SUNR")
            {
                // This is the sunrise
                $sunr = $vals[$key]['value'];
            }
            if($vals[$key]['tag'] == "SUNS")
            {
                // This is the sunset
                $suns = $vals[$key]['value'];
            }
        } 
    }
    

    If you want to see what the output is of variables you need to do:

    echo "";
    print_r($vals);
    echo "";
    
  3. Conversions and such can be ran from the variables, my application didn't require it and I can't spend any time right now making it right. You can figure out what is what from that.

  4. With the change of #2 here are the variables.

    $temp = Temperature
    $wind = Wind Direction
    $theIcon = # of file to display for the icon
    $theSpeed = Wind Speed.  
    $sunr = Sunrise time
    $suns = Sunset time
    
  5. I also have the code for conditions, it's:

    if(($vals[$key]['tag'] == "T") && ($vals[$key]['level'] == 3))
            {
                // Get the current conditions
                $condition = $vals[$key]['value'];
            }
    

You can tweak the output all you want to include those variables, there are LOTS in there. So take your time and look through all the ones that are available.

Take care, Chad President of Get A Supplier

Micah says
Sunday, September 7th, 2008 @ 2:24 PM

I'm working on changing this around a bit to an object-oriented version that will allow for checking multiple zip codes...I had a requirement from a client and loved your script so I decide to go with it and modify it a bit...

Jesús says
Monday, September 22nd, 2008 @ 2:42 AM

Thanks for sharing this, just one note, not every hosting company allows fopen() (like dreamhost), in that case you can change the retrieval of the feed by using cURL

$file = "the weather feed url";

$ch = curl_init();
$timeout = 5; 
curl_setopt ($ch, CURLOPT_URL, $file);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);

$data will contain the xml string

Earpick says
Sunday, November 23rd, 2008 @ 11:45 PM

This is a great script, except I've received the email from Weather.com and in the terms of use it clearly states that you have to include 4 of their partner links below the weather forecast to comply along with, obviously, a Weather Channel logo. I think I'll try Yahoo! weather as the prospect of showcasing links to Disney and eHarmony isn't looking so dandy

Dao says
Wednesday, December 3rd, 2008 @ 5:41 PM

Thanks for the script. I have a quick question. Where could I alter or set up the Cron Job? Thanks!

Rus says
Sunday, March 15th, 2009 @ 6:03 AM

didnt seem to grab dtate... the TXT file was just kind the shell still... not actual info... ??

Scott says
Sunday, March 29th, 2009 @ 4:52 AM

Doesn't work at all. Tried it with php4 and php5. Missing temp, wind speed. Icon doesn't work either.

Patricia says
Wednesday, June 3rd, 2009 @ 11:27 PM

Do you know if it's possible to retrieve next day forecast information and four days after that? If not, I would appreciate it if you can recommend me alternatives. Thanks!

Monique says
Thursday, August 6th, 2009 @ 4:40 AM

Can't you use it for European area? If yes, what to put instead of zipcode?

Manolo says
Saturday, August 8th, 2009 @ 8:34 AM

It's possible extract next 7 days

Golden says
Thursday, October 8th, 2009 @ 5:28 AM

Hi dear,

how can i view all the 26 datas from the $data formated as text (w/o the xml-tags). i will give it in a databank...

thanks golden

Mick says
Friday, December 25th, 2009 @ 3:13 AM

Please update! This is an awesome script but its not working right. I keep getting errors im entering the right path for the root txt file but its not working wtf?

Brian says
Friday, January 15th, 2010 @ 3:22 AM

Are there additional variables? I would like it to display the city name rather than just the zipcode.

Weather says
Wednesday, June 23rd, 2010 @ 6:57 PM

Thanks !

Nil says
Wednesday, November 17th, 2010 @ 7:34 PM

Where can I find information about the meaning of all tags in the xml file?

SATISH says
Monday, February 21st, 2011 @ 3:54 AM

I am using this script , it works with the error

Notice: Undefined index: value in C:\wamp\www\weather.php on line 72

70 foreach ($vals as $key => $i) { 71 72 if ($vals[$key]['value'] != false) {

I have a limited exp. with PHP and couldn't comprehend where variable $i is updated.

Regards.

African Mango In Stores says
Thursday, November 22nd, 2012 @ 7:34 PM

I'm amazed, I have to admit. Seldom do I come across a blog that's both equally educative and entertaining, and let me tell you, you've hit the nail on the head. The issue is an issue that not enough men and women are speaking intelligently about. Now i'm very happy I found this in my hunt for something regarding this.