Sunday, September 5th, 2010

Weather Script v.2
Root page of JMan's Bedpan
A short little bio of JMan and JMan's Bedpan
Some PHP scripts that JMan has written, for public use. MSN Emoticons, Weather, Webcam, Latest Version, Cache, HotScripts
Other random stuff that doesnt belong on any other page. Family Feud Cheat, I *Heart* James, Stupidest Famous Last Words, Funny Search Engine Queries, Funny Euphemisms
Rants by Blakey
A bunch of useful links
. : : Local Weather Forecast

Use it? Show me.


Version II. This magnificent piece of code leeches weather information off of Environment Canada's weather pages, and displays it in a very flexible format on your own website.

   Major updates from the initial version include:
   · English or French Selection
   · MySQL or Flat-File Caching
   · Object Oriented Style
   · Superior Configuration and Customization Capabilites
   · Ability to Show Longer Text Descriptions, and Current Information (if available)
   · Selectable Display Formats Pre-Programmed in
   · POP% Displayed Graphically on the Weather Icons
   · Better Written Code


Explanations:

#.gif - All the different weather image files.
index.php - A redirecting index page to prevent directory viewing.
cache.class.php - Caches fetched information, for quicker results, and less drag on the source.
gpl.txt - A copy of the General Public License.
readme.txt - A quick readme file.
SQL.sql - The SQL table.
testWeather.php - A working example of the script.
weather.class.php - The script page. Include it in the page you want the weather displayed on, then call its functions.


EXAMPLE: (May not be exactly as shown.)

<?
include_once("weather.class.php");

$weather = new getWeather();
$weather->dbUser="root";
$weather->dbPass="";
$weather->dbName="db_name";
$weather->cityCode="on-82";
echo 
"<table cellspacing=1 cellpadding=1>";
if (
$weather->fetchData()){
    echo 
    
"<tr><td colspan=3 align=center>\n\n".$weather->showWeather().
    
"\n</td></tr>";
}
$weather->cityCode="bc-74";
$weather->dispFormat=1;
if (
$weather->fetchData()){
    echo 
"<tr valign=top><td>\n\n".$weather->showWeather()."\n</td>";
}
$weather->cityCode="nf-24";
$weather->dispFormat=2;
$weather->longText=true;
if (
$weather->fetchData()){
    echo 
"<td>\n\n".$weather->showWeather()."\n</td>";
}
$weather->cityCode="nu-21";
if (
$weather->fetchData()){
    echo 
"<td>\n\n".$weather->showCurrent()."\n</td></tr>\n";
}
echo 
"</table>";
?>

 Kitchener-Waterloo
Wednesday
Wednesday night
Thursday
Friday
Saturday
Sunday
Sunny With Cloudy Periods
Clear
Sunny With Cloudy Periods
Sunny
Sunny
A Mix Of Sun And Cloud
Sunny With Cloudy Periods
Clear
Sunny With Cloudy Periods
Sunny
Sunny
A Mix Of Sun And Cloud
High 25°C
Low 12°C
High 26°C
High 28°C
Low 14°C
High 28°C
Low 16°C
High 26°C
Low 14°C
Last Updated: August 16, 2006 @ 11:00 AM EDT
Vancouver
Wednesday
Sunny With Cloudy Periods
Sunny With Cloudy Periods
High 21°C
Wednesday night
A Few Clouds
A Few Clouds
Low 13°C
Thursday
Sunny
Sunny
High 26°C
Friday
Sunny
Sunny
High 27°C
Low 13°C
Saturday
Sunny
Sunny
High 26°C
Low 13°C
Sunday
Sunny
Sunny
High 25°C
Low 14°C
Last Updated:
August 16, 2006
11:00 AM PDT
St. John's
Wednesday
A Few Showers
A few showers ending late this afternoon then cloudy. Amount 2 to 4 mm. Wind south 30 km/h gusting to 50. High 21. UV index 3 or moderate.
High 21°C
Wednesday night
Chance Of Showers
Cloudy. 40 percent chance of showers this evening. Fog patches developing near midnight. Wind south 30 km/h gusting to 50 becoming southwest 20 this evening. Low 16.
Low 16°C
POP 40%
Thursday
Chance Of Showers
A mix of sun and cloud. 40 percent chance of showers in the evening with the risk of a thundershower. Wind west 20 km/h. High 24.
High 24°C
POP 40%
Friday
Sunny
Sunny. Low 14. High 20.
High 20°C
Low 14°C
Saturday
Sunny
Sunny. Low 14. High 20.
High 20°C
Low 14°C
Sunday
Chance Of Showers
A mix of sun and cloud with 40 percent chance of showers. Low 15. High 21.
High 21°C
Low 15°C
POP 40%
Last Updated:
August 16, 2006 @ 11:00 AM NDT
Current Conditions
Iqaluit Airport
Cloudy
Cloudy
08 /16 /06 1:00 PM EDT
Temperature
6°C
Pressure/ Tendency
99.8 kPa↑
Visibility
13 km
Humidity
97 %
Dewpoint
5°C
Wind
SE 8 km/h
Yesterday
Max Temp.
9.9°C
Min Temp.
3.4°C
Precip Total
2.0 mm
Regional Normals
Max Temp.
10°C
Min Temp.
3°C
Today
Sunrise
4:30
Sunset
20:47
Moonrise
19:43
Moonset
18:10


-- DOWNLOAD: PHP4    PHP5 --
2341 Downloads To Date

HotScripts.com
Rating: 4.54 / 5
Votes: 39
Hits: 4,296

LASTEST 5 UPDATES: [Show All 17 Updates]
March 11, 2009
Version 2.3.5:
There is a new function, array_snip. It does the legwork for the workaround which deals with the extra Tomorrow Night, or X-Day Night long-text Descriptions.
Now the long descriptions should work, and show the right data for the right days again.


April 10, 2008
Version 2.3.4:
Yeah, I had a typo in the name of the cache class file. Fixed.


April 7, 2008
Version 2.3.3:
Added $fontColour so that you can change the font colour of the displayed weather data, from the web page's default colour, to whatever colour you want.


March 19, 2008
Updated to work with new *slightly* different HTML code. Looks like their doing cross browser compatibility stuff.

Oh yeah. And also now, FLAT FILE caching should be supported. Set $useMySQL to false, and make sure you have a properly permissioned cache directory waiting. This is new to me, so it may be buggy.


February 12, 2008
Another big one. Now, you have the option of displaying your weather in French. A bunch of changes were made to accommodate.
Another major change is the ability to show the POP% on top of the weather icon, instead of as green text.
Fixed error with PHP4 version not having array_diff_key().
Cleaned up the code in some spots, and made some other small changes.


D
12/23/2009 04:23 PM
Does this PHP used the public weather pages or the XML library they offer: http://dd.weatheroffice.ec.gc.ca/
JMan: It does not make use of the XML pages. From what I remember, when I was looking at the XML, it would have been a little bit less of a file to download, but there wasn't any information about the relevant weather icons, so I didn't bother with it.

Bill
03/12/2009 12:14 PM
An idea to improve the readability.

Start on a new line when appending Tonight: text.
Maybe bold Tonight:

Any thoughts.

Bill
03/12/2009 11:08 AM
Version 2.3.5 has fixed the Tonight problem. The Wiarton ON. forecast is now displaying correctly.

Thanks for all your work.

jared
03/11/2009 11:57 PM
Thanks - looks good :)

jared
03/11/2009 01:14 AM
Happening right now again ;)

Seems if there is like Today / Tonight & then the days, or Today Wednesday / Wednesday night it gets mixed up

kjh.ca/iphone-1.png
kjh.ca/iphone-2.png

as well as
kjh.ca/ff-1.png
kjh.ca/ff-2.png
JMan: Okay. I think I've done a workaround. And in my brief testing, it seems to work fine. It checks if there is a X-night, and if there is, it appends that text do the day portion. This way you don't need another block, but you still get the text.

Also, I noticed that I started working on a version with capabilities for an external CSS... But I never finished it, and it was lost on the back burner. If I have time, I'll try and get that finished too.

Either way, let me know how things work.

Jared
03/10/2009 08:45 AM
Looks like your excellent script needs a minor update as the weather for Calgary - possibly due to the today tonight - is offset by a day in the descriptions

Rock on!
JMan: ALright. Umm. Either I can't replicate this, or I don't know what you mean. Sorry.

jared
12/07/2008 03:16 PM
FYI, there is a weather warning for Calgary AB right now...

I've saved the page if you want it :)

Thanks again!
JMan: I'll see what I can do.

jared
11/30/2008 03:54 PM
Will do - much thanks :D

Finally a way to have minimal weather data from EC!

Thanks again for all your hard work :D
JMan: No problemo.

jared
11/30/2008 01:20 AM
Any chance of linking to an external CSS instead of stating it in each object?

Trying to make it display better on my iPhone as I check the weather many times daily (I work outside)...

Thanks,
JMan: The way I wrote it, I use inline-style tags. This was done because there were a bunch of different style types, and this way just seemed to simplify things. You are able to change those as easily as I could though. A simple Find & Replace in any basic text editor will do.
If I ever get a bit of tinkering time, I'll give 'er a go.

patrick
10/20/2008 05:15 PM
Hi there, thank you for the great code!
I can't seem to make this work using a Flat-File which would be best for me!
Can you provide more explanation?
Thanks a bundle!
JMan: Do you get any errors, or does it just not work at all? Make sure that the $cacheDir directory exists, and is writable. Other than that, I don't really know what to tell you right now. Hope this helps.

Nick
10/17/2008 10:43 AM
Was the weather script supposed to show Weather Warnings as well? The location I have setup to display is under a Heavy Rainfall Warning, but the script just shows the regular forecast text.
JMan: I don't believe I had a chance to do any testing with Weather Warnings when I wrote this. If they're still displayed, please send me an email with a link, or the HTML or anything like that, and I can probably get something going.

Canada
08/07/2008 06:44 PM
Is it possible to use this script for other country's also?
JMan: Like I've told everyone else who has asked the same question, Environment Canada only provides weather data for Canada. Thus, my script as well. Sorry.

Bill
04/10/2008 11:44 AM
Problem with weather.class.php ver 2.3.3 (PHP 5)

Warning: require_once(newCache.php) [function.require-once]: failed to open stream: No such file or directory in C:\***

I changed "require_once "newCache.php");" to cache.cla--.php and script runs ok.
JMan: Whoops, you caught me. That's just hte name of a local 'temp' or 'beta' version that I tinker with, when things need to be tinkered. Thanks for catching that. Fixed in version 2.3.4

Mike
04/07/2008 06:21 AM
Is it possible to change the font color??
JMan: Umm, actually no. It wasn't. But now, it is. New in version 2.3.3, just for you, the option to change the font colour. $fontColour lets you set the font colour for everything contained in the weather tables. Tada! Enjoy.

Bill
03/20/2008 08:49 AM
Thanks for Version 2.3.2
Works great.
JMan: Glad to help.

Nick
03/20/2008 12:48 AM
Flat file works... will keep an eye out for errors.
JMan: Good, Thanky.

Nick
03/19/2008 08:34 PM
I to am not getting any results as of today. Anything you want me to look at for debugging purposes?
JMan: Should be fixed now. If you'd like, you can play around with the flat-file caching, and see what errors you can get it to throw, and if it works, or screws up or anything. I already know I need to write an error handler for if there Isn't a chmodded 777 (I think) directory. I did this update a while back, and never really finished it. Tonight I needed to update the script though for the update. So hopefully it works.

juan
03/19/2008 04:52 PM
Suddenly, nothing is showing today.
It was fine since I ran it for two months or so.
Are there changes in the EC code?
JMan: Thanks for pointing this out. Should be fixed now. Updated the script with my thumb-broken hand (I broke my thumb at work today, yay). Thats how much I care :P.

jared
03/02/2008 02:01 AM
Hey JMan - great work! I would imagine that it has taken a bit of your time ;)

Any chance of making a Samurize plugin? There's one that leeches data from accuweather(?), but that is no where near as accurate.

Check out Samurize as it rocks for making desktop widgets, and it would benefit GREATLY from having an EC plugin / script.

Again, much thanks.
JMan: Well, unfortunately, since I do not use Windows, writing a Samurize plugin would be a chore for me, and in the end, I wouldn't even be able to use/enjoy it.
I had a brief look at their weather plugins, and I found one called 'WeatherCan v2.2', which uses data from Environment Canada. Maybe that could work for you. Best of luck.

Bill Wood
02/17/2008 09:53 AM
Probability % on the weather icon is a nice touch.

Thanks.
JMan: I thought so too. Took me a while to figure out what I was doing wrong though. It would work in most browsers, but not all. Then I remembered that transparencies in PNG files arent fully supported, so I had to switch back to GIF images for the overlays. Once I figured that out, it worked properly in every browser.

Nick
02/17/2008 01:30 AM
The POP in the images looks great... the vertical column layout works well with my existing WDLive stuff.. check it out http://zepplin.dyndns.org:8080/index2.html
JMan: Awesome. Looks great. A couple suggestions though:
-On line 33 of that page, change <td> to <td valign="top">
-Also, try playing around with the font sizes, and maybe the font face of the weather script. Try to get something that looks a bit closer to your flash app.
-Finally, play with the headColour. See what white, or a light blue would look like.
My two cents.

juan
02/12/2008 02:38 PM
Fatal error: Call to undefined function: array_diff_key() in /home/cfco/public_html/newsite/weather.cla--.php on line 491

hi jman, i honestly think this is a great script. except that i get the error above. i downloaded the php 4 version of your script. i'm not sure what php version my remote host runs, if it matters at all. anyway, i hope you can help me with this error. thanks.
JMan: Oops. array_diff_key() is only for PHP5 >= 5.1.0. Fixed in the latest version.
And the easiest way to figure out what version of PHP you have:
<?
echo phpversion();
?>

Bill Wood
01/30/2008 03:17 PM
Thanks for all your hard work.
Great script
JMan: :)

Nick
01/27/2008 06:06 PM
Great script.. took me a bit to figure out that my php didn't have mysql support enabled.

Looks good! Keep up the good work!
JMan: Thanks.

Luc
01/24/2008 04:56 PM
The change at EC had a major effect on your script as it doesn't work anymore. Too bad ! Was pretty cool and worked as a charm. Do you intend to change it to match EC changes ?
JMan: UPDATE:
Working on it. Give me a short time. I'm almost done. I just don't have enough time in the day to work on it. Mostly done now though. Stay tuned.

Bill Wood
01/21/2008 11:11 AM
Hi JMan:

EV Canada is showing this notice on its 5 day weather pages.

" On Jan 23 - New left menu and 5 day weather pages!! "

Don't know if the change will affect your weather script but thought I would give you a heads up.

Bill

JMan: TO BE CONTINUED...
DUN DUN DUN!!

Mike
08/16/2007 11:24 AM
hey JMan. Just downloaded. Great script! Except, I cna't get the City Name, Current Conditions or Last Updated: to display. Current Conditions only shows a 0. The others are just blank. showWeather works fine. Any ideas?
JMan: Hey Mike,
It's hard to say what's going wrong with your setup without actually seeing the script you're using.
A couple of things though:
-Make sure you're using the latest version (if you just downloaded it, then there should be no problem here)
-Try viewing the example testWeather.php on your server, and see if that works
-If the testWeather page works, then look at it's code, and see how it is different from yours
-It is also possible that Environment Canada's page does not have the information you're requsting for that particular city. If this is the case, you will not be able to get the data, since it is not there. Go to the city's webpage (at http://www.weatheroffice.ec.gc.ca) and verify that the information you're looking for is actually there.

Joel
07/15/2007 10:50 AM
Hi,

This script won't work for me. I've tried both the php4 and php5 scripts. Any help?
JMan: If you send me (via email) some error logs, or a description of what it is (or isn't) doing, I might be able to help you. Otherwise, no. No help. =)

Bill Wood
06/24/2007 11:04 AM
Hi JMan:

Glad to see your website back on line.

Your new June 16 2007 Weather.class.php ver 2.0.9 works with no problems.

I don't know if this is EV Canada's final weather page version or if more changes are coming.

Found this on http://www.weatheroffice.gc.ca/mainmenu/faq_e.html website.
"March 14, 2007 - New Graphics on Our Web Site Users to our web site will notice significant graphic changes as our web site adopts new standards. This change represents the first phase of web improvements as we align the site with the new norms established for Government of Canada web sites. Further changes can be expected over the next several months as we address outstanding requirements in the new web standard."

Hope you have the time to keep up your great work on this script.
Bill.
JMan: Glad that it works, and now that (mostly) everything is back up and running, it should be easier to maintain.
I checked the weathericons over, and they're still the same as the ones in the package, so that's not a problem yet. And hopefully the changes they make won't be too significant to warrant a v.3 of the weather script, but we'll have to see.

Thanks for the support,
-JMan

Stephen
12/21/2006 04:06 AM
Hay have you ever thought of caching the info instead of the db?...Just wondering cuase I'm currently creating a website for someone who can't have dbs :(
JMan: Thought about it, yeah. Done anything with those thoughts, no. Recently started a new job, so I don't have time to start any new projects at the moment. Shouldn't really be too hard though. Instead of writing to MySQL, write to a file. If someone writes a script to do this, I have no problem hosting such a script.

karl
12/16/2006 11:02 PM
I am missing the newWeather.php at include_once("newWeather.php"); within the example!
JMan: Crap. Typo.
Changed to 'weather.class.php'. 'newWeather' is what I was calling it when I first started making the version 2. Sorry for the inconvenience.

Bill
12/14/2006 09:36 PM
Thanks JMan; revised weather.class.php works great.
JMan: Great. Here's hoping they don't change their format around for atleast another year *fingers crossed*.

Bill
12/13/2006 08:21 PM
Hi JMan:

I have been using your great Weather Script v.2 on my website and it has been working with no problem until today. Today showCurrent() displayed only a "0" and showWeather() displayed ok with the exception that "Last Updated:" is blank.

I have save some of EV Canada's past scripts and it looks like EVC has made a change in the time/date format.

From a Nov. 30/2006 script:
<DIV id=cityobserved>Observed at: Wiarton Airport 30 November 2006 7:00 PM EST</DIV>

From today's Dec. 13/2006 script:
<div id="cityobserved">Observed at: Wiarton Airport 7:00 PM EST Wednesday 13 December 2006</div>

A while back EVC Wiarton did not display the "Current Conditions" for a day and showCurrent() return true displaying only a "0". Shouldn't showCurrent() return false in this case?

Do you have a small logo that I can add to the credit links on my website?

Keep up the great work,
Bill.
JMan: Thanks for pointing this out. It has been fixed; download the new version for a working copy. They changed their date/time format, so my regex expressions no longer matched what they had properly, so it was bunging up the rest of the script.
As for the 'should it return false' question, I would have to say no. The script relies on the fact that it can match the Environment Canada data properly, so if that doesn't, the script isn't going to run properly. That's just how things go. If you want to rewrite parts of my code, I'll gladly post useful revisions on my website.

Tiny BannerAnd for a logo, I did make one up a while back, and it can be found by reading through the guestbook, or here.

Dan
11/14/2006 11:12 AM
Hi,
Wanted to say that this is the best script I've ever used for weather! I was wondering though, what would I need to change in the script to pull american weather feeds? I want this to be used by both my american and canadian members.

Thanks in advance.
JMan: Well, since this script gets its information from Environment Canada's information, they don't have any data for american cities. In short, this script simply wont work for out-of-canada cities, as any other website would present its data in a different format than how Environment Canada present's its data, and my script wouldn't know how to read it.

Sheri
10/09/2006 08:16 PM
THANK YOU JMAN (L) Works like a charm ({)
JMan: No problem. If you have a spot somewhere, put a link to my site, or the script page or something, and that'll be good.

Sheri
10/09/2006 04:57 PM
Hi again Jman.
Thanks for the response. That didn't work for me so I created (to the best of my meager abilities LOL) a script to pull out the info. Right now it calls the file_get_contents again and is not cached. If you could look at this and possibly a--ist (fingers crossed ;-P ) with the integration (and probably clean up) into your weather script that would be FANTASTIC.
Here is the script:

$contents = file_get_contents("http://www.weatheroffice.ec.gc.ca/forecast/city_e.html?on-82&unit=m&b_templatePrint=true");
$new_contents = strip_tags($contents,'<dl>,<dt>,<dd>');
$newer_contents = strstr($new_contents,'<dd>');
$brand_contents = strip_tags($newer_contents,'<dd>,</dd>');
$brandnew_contents = strpos($brand_contents, '>', 4);
$the_temp = substr($brand_contents, 0, ($brandnew_contents +1));
$original = array("<dd>", "</dd>");
$proper = array("<span style="padding-left:5px;color:#000063;">", "</span>");
$image = strstr($contents,'<img');
$new_image = stripos($image, '/>');
$the_new_image = substr($image, 0, ($new_image +2));
$the_final_temp = str_replace($original,$proper,$the_temp);
echo '<div>'.$the_new_image . $the_final_temp. '</div>';
JMan: Alright. If that's all you're after, then download my Cache Script, and following code should do the trick:
include ("cache.php");
$cached = cache("tinyWeather");
if ($cached){
   $tinyWeather = $cached;
}else{
   $contents = file_get_contents(
"http://www.weatheroffice.ec.gc.ca/forecast/city_e.html?on-82&unit=m&b_templatePrint=true");
   preg_match_all('/<img class="currentimg"( src.*?")\/>.*?Temp.*?<dd>(.*?)<\/dd>/s', $contents, $result, PREG_PATTERN_ORDER);
   $tinyWeather = "<div><img".$result[1][0]."><span style=\"padding-left:5px; color:#000063;\">".$result[2][0]."</span></div>\n";
   $cached = cache("tinyWeather", 3600, $tinyWeather);
}
if ($tinyWeather) echo $tinyWeather;
else echo "Weather Data Unavailable.";

Sheri
10/09/2006 12:31 PM
Hi Jman!
I love the script and your site :-)
Is there a way to display just the image and the temperature? Would like to display this small amount of info on the frontpage with a link to the full weather conditions. Thanks for the script. It's FAB!
JMan: As written, there isn't a way to do that. This gets you close:
$weather->showCity=false;
$weather->showUpdt=false;
$weather->showBorders=false;
$weather->showBlocks = 1;

From there, you'll have to manually comment out the lines which output the day name, and the short description (if desired):
Change line 356 to:
if ($g==0) $this->weatherInfo[$g][$b] = "";
and make the following line number 359 (insert this line entirely):
elseif ($g==2) $this->weatherInfo[$g][$b] = "";
If this isn't what you're looking for, email me back, and ill do my best to help.

Dawson
10/05/2006 07:01 PM
Hey man, love your script... I modified it a bit to output some xml. That way I made a nice flash movie for the current conditions. Excellent once again.
JMan: Awesome. I'd love to see that in action. I used to try to make some crap in Flash, but that's all it ever was. Crap. Now I just stick to PHP. *Envy*

Dale
09/12/2006 08:25 PM
Know of anyone that has modified it to work with Curl? Dreamhost doesn't seem to support it the current way.
JMan: Replace the line containing file_get_contents() with something like this:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,
"http://www.weatheroffice.ec.gc.ca/forecast/city_e.html?" .
$this->cityCode . "&unit=" . $realUnits . "&b_templatePrint=true");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$contents = curl_exec($ch);
curl_close($ch);

There is now an option in the configuration to use cURL.

Maxey
08/17/2006 01:44 PM
I have reinstalled three times, but the data is not being written to the sql database. However, The fetching works wonderfully.
JMan: The first of the errors =). I rushed a bit to make this public, and I somehow botched the mysqlConnect() function in cache.class.php. Fix:
private function mysqlConnect(){
  if (!$this->mysqlConnection){
    $con = @mysql_connect($this->dbServer, $this->dbUser, $this->dbPass);
    if ($con){
      mysql_select_db($this->dbName);
      $this->mysqlConnection=true;
      return true;
    }else return false;
  }else return true;
}
This will be fixed in all versions downloaded from now on.

Add a Comment (Max: 1 Per Hour)

Please make sure that your comment pertains to this page. If it has nothing to do with this page, I'll just delete it. If you want, you can send me an email here: jman@bedpan.ca.

Name :
Email or URL :
Verification :
All fields are required for submission. Entries with blank fields will not be posted.

disclaimer:
the comments posted throughout my site are submitted by other users.
the things said are not endorsed by me, and do not represent my opinion.