# Solved: PHP Weather



## Fanis (Jul 31, 2003)

New to php. Tried to add weather to site using a "simpler" script that doesn't require MySQL (http://www.windal.net/php/showsource.php?phpfile=weather.php) 
Getting the following errors when I test the page:

Warning: filemtime(): Stat failed for ..._mywebsite_/www/php/metarcache..en (errno=2 - No such file or directory) in ..._mywebsite_/www/get_weather.php on line 90

Warning: fopen(..._mywebsite_/www/php/metarcache..en): failed to open stream: No such file or directory in ..._mywebsite_/www/get_weather.php on line 109

Warning: fwrite(): supplied argument is not a valid stream resource in... _mywebsite_/www/get_weather.php on line 110

Warning: fclose(): supplied argument is not a valid stream resource in ..._mywebsite_/www/get_weather.php on line 111​
Do I need a separate file named "metarcache"? If so, how do I find the type of file it needs to be and do I need to replace 'DOCUMENT_ROOT' with the name of my website directory? Here is the code surrounding the line with the first error.

$cacheFName = getenv('DOCUMENT_ROOT') . "/php/metarcache" . ".$station" . ".$lang";
//update cache no more frequently than once per hour...
$updatePeriod = 3600;
if (!$doNotCache && (time() - filemtime($cacheFName) < $updatePeriod))​
Thanks for any help,
Fanis


----------



## haswalt (Nov 22, 2004)

you do not need to swap "DOCUMENT_ROOT" with anything, that is a php specific command environment reference.

The last two error (fwrite and fclose) are becuse of the failed fopen, get rid of that and the last two will go.

out of interest why are you using getenv()?

also have you noticed that the file you are trying to open is metacache..en?
Find out what value $station has....

Could i see the rest of the script please as i do not know what variables have what values.

Harry


----------



## Fanis (Jul 31, 2003)

Harry,

Thank you for you reply. All I know is that this was the free code available at the link shown above (original post) - the entire code is there: http://www.windal.net/php/showsource...le=weather.php

It says in the comments of the script that all I needed to do was put in the METAR station, which I did.

I'm wondering if I should code it to "donotcache" as it discusses in the comments instead. Unfortunately, I'm too new to php to even know how to revise the code where the comments say it should be set to "1".

Would you please take a look at the linked script and the comments and give me your suggestions?

Thanks,
Fanis


----------



## cnelson04 (Dec 29, 2003)

I get an error massage when trying to view that link, (404) . -cnelson.


----------



## Sequal7 (Apr 15, 2001)

To be clear, you are calling the script in the area on the page you want the weather to show, and it is a .php page? (you cant use .htm or .html as the filetype)

Example index.php has 
<?
$station = 'KRIV';
include 'weather.php'; ?>

where you want the weather form to show. The script only requires that you change one variable (both on the weather.php and your include source) and that is the weather station in your area. A complete list of stations is here:
http://adds.aviationweather.noaa.gov/metars/stations.txt


----------



## Fanis (Jul 31, 2003)

Sorry guys, looks like he moved the file. Here's the new link

http://windal.net/php/showsource.php?phpfile=weather.php

Thanks again,
Fanis


----------



## Fanis (Jul 31, 2003)

JoyCo,

Sorry, I'm not following you completely. My page is a php file and I already have the station code. The weather data is coming up in the test, but it is also showing those errors that I posted. Finally, I thought from what I saw in his script that the page is a .php file, including html (inside the body) with a link to the php lower on that same page. So, do I need two pages?

Fanis


----------



## Fanis (Jul 31, 2003)

Okay, sorry again - the link that pastes into the message isn't the same as the one on the address bar. If you still can't link to the earlier post, here is the link from google to the page: 
http://windal.net/php/showsource.php?phpfile=weather.php


----------



## Sequal7 (Apr 15, 2001)

The weather.php page in your link is actually code, you aren't supposed to be calling it as a webpage, it is intended to be a SSI (Server Side Include)

Here's an example of how it works.
Download the source code weather.php page from the link you provided and upload to your server
on *any* webpage you want the weather to show, (must be a php page) you simply place this (it is called a *S*erver *S*ide *I*nclude):
<?
$station = 'KRIV';
include 'weather.php'; ?>

where *$station = 'station' * enter your station code from this link
and *include 'weather.php'* is the location of the weather.php page relative to the document the SSI is on

I created a blank php page, named it weather1.php page with only this in it;
<?
$station = 'KRIV';
include 'weather.php'; ?>

and it returned the exact sample on the site you got the code from.

Here's my working sample.
http://server1.oakbayfire.com/test/weather1.php


----------



## zero11 (Jul 4, 2005)

Are you saying that we just upload the weather.php as it is and use the above code by changing the station you want, and you would get the weather of your city ?

Cool......let me try that on my website.


----------



## zero11 (Jul 4, 2005)

Fanis said:


> JoyCo,
> 
> Sorry, I'm not following you completely. My page is a php file and I already have the station code. The weather data is coming up in the test, but it is also showing those errors that I posted. Finally, I thought from what I saw in his script that the page is a .php file, including html (inside the body) with a link to the php lower on that same page. So, do I need two pages?
> 
> Fanis


Create a html page as Sequal7 says and call your weather.php. It should give out the weather without any errors. Good Luck.


----------



## Fanis (Jul 31, 2003)

Thank you Sequal7 (and anyone else who tried to explain it). Your thorough explanation and providing a sample page really helped!!! I didn't realize that the code provided by windal needed to be uploaded just as a "server side" to be called from a newly created page. That will actually be easier because I already have a web page where I only wanted the weather to be added inside a little box in the upper corner! 

Thank you again!!!!


----------



## Sequal7 (Apr 15, 2001)

zero11 said:


> Are you saying that we just upload the weather.php as it is and use the above code by changing the station you want, and you would get the weather of your city?


Yes, that is exactly what that code does.



Fanis said:


> Thank you Sequal7


 You are welcome, sometimes going right back to the beginning with explanations helps, as in this case.


----------



## zero11 (Jul 4, 2005)

Sequal7 said:


> Yes, that is exactly what that code does.


Sorry, it does not work. I am not sure what more I have to do. I just added the following script

<?
$station = 'VOBG';
include 'weather_php.php'; ?>

uploaded the weather_php.php to my server..........Does not work. Nothing happens - no error messages as well.


----------



## Sequal7 (Apr 15, 2001)

You need to call it in a php page (wont work with a .htm or .html file type)


----------



## php guy (Dec 17, 2004)

Sequal7 said:


> You need to call it in a php page (wont work with a .htm or .html file type)


Unless you set up .htm/.html pages to be php processed with .htaccess or host services.


----------



## zero11 (Jul 4, 2005)

php guy said:


> Unless you set up .htm/.html pages to be php processed with .htaccess or host services.


sorry I do not understand this. Should I call my .htm page as .php page ??


----------



## php guy (Dec 17, 2004)

Yes, the easiest way would be to rename all your .htm or .html pages as .php pages. So page1.htm would become page1.php Just be sure to make links to page1.php instead of page1.htm.


----------



## Sequal7 (Apr 15, 2001)

Don't rename all pages to a .php extension, only those are actually processing or including scripts.


----------



## Sequal7 (Apr 15, 2001)

Fanis;
I understand your still having problems with the weather script 
I have attached it as a php file (in case your conversion wasn't correctly done or damaged from the text file provided inthe link) Make sure yoto upload it in ASCII (or auto) with your ftp program to your server. Sinply opening this file with Wordpad, Microsoft Frontpage, MSword, notepad eetc can damage the file, as can incorrectly set uploading formats so be cautious. As a precaution, always use a HEX Editor such as Ultraedit or Dreamweaver for creating/editing php files.

Try this php file in the zipped folder and see if it helps.


----------



## Fanis (Jul 31, 2003)

Thanks for all your help. I got it working, except that I'd like to decrease the size of text in the data. Is that something I can do in the original weather.php or in my webpage.php that calls weather.php?

Fanis


----------



## Sequal7 (Apr 15, 2001)

Hello
Glad you got the code to work, Curious..How did you get it working?


Fanis said:


> except that I'd like to decrease the size of text in the data.


*If all you mean is change the text size, and not what is displayed such as barometer or wind etc;*
Then all you need to do is create a css style or page, then change line 677 of the weather.php class source. You will notice it is currently using a class named "metartable"
$res .= "<table class=\"metartable\">\n";
Either create that class in your css sheet, or rename that to a style you want.

*If you mean you want to remove or add items that are displayed;*

You need to modify the weather.php page.
Look for and edit the area on the weather.php page below (if your using ultraedit or dreamweaver this begins on line 677) otherwise it is near the bottom of the code
To comment out (or not show the data,) add // in php at the beginning of the line.
eg. //Comment's the entries in the array below

foreach(array(
//'STATION',
//'OBSERVED',
//'NOW',
//'AGE',
'WIND',
'VISIBILITY',
'CLOUDS',
'TEMP',
'DEWPT',
'HUMIDITY',
//'BAROMETER'


----------



## Fanis (Jul 31, 2003)

Sequal you have been WONDERFUL! I apologize for making this thread so long, but I'm learning so much and you've peaked my curiousity! Please permit me to ask three final questions and I'll get out of your hair!

First, though, to answer your question about my "fix", I simply inserted code of $doNotCache=1; on the line above the $cacheName. Okay, now for my questions:

1. I do use Dreamweaver now, but most of my experience is in html. I haven't learned much about CSS either. The text size/font is what I want to change. Since I didn't download a CSS file, do I just create a class using that name or change that in the code to match the CSS in my template?

2. You mentioned items that are displayed in the array. I wanted to add 'windchill' and 'conditions' to the results, but I failed when trying to add WINDCHILL. I added 'WINDCHILL' => 'WINDCHILL' into the "definition" of the array and then 'WINDCHILL' in the print function's array. Also tried adding get_wind_chill and get_windchill to the process metar function - all I got were errors. Do you still have the code to see if you can tell what I did wrong?

3. Finally, I re-read one of your earlier responses to another member in the thread where you said only the pages that process or include scripts need to be .php extensions. So, I originally planned to have a "weather window" beside a logo on every page of my friends website. Do all of the pages need to be php, do I need to put it into the template and if so should the template take on the php extension, or is it only the page containing the "application" - the one being called (not the page calling) that needs the php extension?

Thanks & I will try to make this my final questions on this topic.
Fanis


----------



## Sequal7 (Apr 15, 2001)

Hello Fanis

The header no-cache problem was what I expected, so the downloadable zip I provided I uncommented that in the script. (would have fixed your problem as well)

1) for the CSS; I would create a class in your .css file for your site if your using a link to a seperate css file. If not, then you can still define it in the of the webpages it will display on (not the weather.php page)
heres an example of smaller bold blue letters for a css page

```
.metartable{
	font-family: sans-serif;
	font-style: italic;
	font-weight: bold;
	color: Blue;
	font: 10pt;
}
```
and for a n include in the page's 

```
<style type="text/css">
<!--
.metartable{
	font-family: sans-serif;
	font-style: italic;
	font-weight: bold;
	color: Blue;
	font: 10pt;
}
-->
</style>
```
2) The Metar has specific data that can be processed. Unfortunatley they have windchill in their factors, but it is calculated and translated as dewpoint.

3) you will need to have every page that displays the weather.php SSI include page as a php page itself. The template file as a php should suffice.
There is no real harm having every page a php ectension, it is just a server rule to not set non script pages as php or asp or jsp etc.


----------



## Fanis (Jul 31, 2003)

OH!!!!!!!! I see the difference in your code now. I completely missed the uncommenting of the header. 

Thanks again for all the help. I'll drop you a note when I get the site up and running if you'd like to see the results.

Much gratitude!!!!!!!!!!!!!!
Fanis


----------



## zero11 (Jul 4, 2005)

php guy said:


> Yes, the easiest way would be to rename all your .htm or .html pages as .php pages. So page1.htm would become page1.php Just be sure to make links to page1.php instead of page1.htm.


Sorry guys, I could not get back to your earlier as I was out of town.

BTW, it does not work by just renaming a htm file to a php file. When I rename and view it on my browser, I see the code instead of the result. There must be something I am not getting or doing it wrong.


----------



## Sequal7 (Apr 15, 2001)

zero11, can you provide your html for the linking php page (not weather.php), or a link to it?


----------



## zero11 (Jul 4, 2005)

Sure

HERE IT IS


----------



## Sequal7 (Apr 15, 2001)

zero11
Couple of problems
1) your page extension is .html, it should be .php
2) your link to weather_php.php doesn't exist on your server at the location you input.
3) it appears that bangelore doenst transmit the weather Metar anymore, so the form wont show even if it was working. You could try a different station that is near you.

Download the zipped page that I provided earlier and unzip it. Using your FTP program, upload that file to your webserver then rename it weather_php.php

Connect to your server with your ftp client and change the weather.html page from .html to .php (rename the server file by right clicking it, then choosing "rename" and type weather.php)
Update your links, or css for links to that weather.html page

Here it is working... using the weather staion at HYDERABAD India (I dont know if that is close to you or not) but you can try the station in the weather office link to see if any close will present data.

Sorry about copying your webpage, I will remove this link from my site once you see what I have done.


----------



## zero11 (Jul 4, 2005)

Thanks a lot Sequal7. You have gone through lot of trouble doing this for me. Wow this is something very nice of you. thanks a lot.



Sequal7 said:


> zero11
> Couple of problems
> 1) your page extension is .html, it should be .php
> 2) your link to weather_php.php doesn't exist on your server at the location you input.
> 3) it appears that bangelore doenst transmit the weather Metar anymore, so the form wont show even if it was working. You could try a different station that is near you.


1)I did rename it as .php, but as i said earlier it was not giving me the result but the entire code itself.
2)I had removed the link as it was not working. sorry about that....I should have uplaod it when I gave you the link.
3) OH ! that's bad news. Looks like you know your Geography well. Yes Hyderabad is also south of India like Bangalore.

Thanks once agian for all your help. You are simply THE BEST.


----------



## zero11 (Jul 4, 2005)

In the page that you uploaded where is the php code ??

I see only a table with the following code


WINDS at 8 mph (13 km/h) VISIBILITY13 miles CLOUDSmostly cloudy TEMP99F (37C) DEWPT55F (13C) HUMIDITY24% 

How are the results populating to his table ?? I dont understand.... could you explain ? I dont see the php code that calls the weather_php.php.


----------



## Sequal7 (Apr 15, 2001)

You wont see the SSI code on the page because it is processed first, and written as html, thus the term *P*re *H*ypertext*P*rocessing, it processes the code and displays it as html so the code is not actually ever seen on any php page.

What I did;
Changed the page the code was called on (your page named weather.htm) to weather.php (I used the code you provided in the SSI on your page, and only changed the anme to .php). 
I had to change the weather station from Bangalore (VOBG) to HYDERABAD (VOHY) in that server side include on weather.php as Hyberabad station was working (as I said, the station you were trying in Bangalore was not working)

Uploaded the files to my server, and viola!

The code you wnated to see is still on the page as;
<?
$station = '*VOHY*';
include 'weather_php.php'; ?>

I had to change the station to VOHY which is the code for HYDERABAD India. 
The zipped file has uncommented headers no-cache, which is required for the script to run, that could als bo a reason for the errers you seen on the first tries. Downlaod the zip file, upload to your server the php page, rename it *on your server* using your ftp client weather_php.php (If you rename it on your local computer it may corrupt the file so do it on your server)

If you replicate this and get errors, it could be that your server does not support PHP. 
You can run a script to test for php, copy and paste this in notepad (or preferrably a Hex editor like ultraedit) name it info.php and upload to your server, then browse to it (www.yourserver.com/info.php)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

Phpinfo

<?php
// prints e.g. 'Current PHP version: 4.1.1'
echo 'Current PHP version: ' . phpversion();

// prints e.g. '2.0' or nothing if the extension isn't enabled
echo phpversion('tidy');
?>
<?php

// Show all information, defaults to INFO_ALL
phpinfo();

// Show just the module information.
// phpinfo(8) yields identical results.
phpinfo(INFO_MODULES);

?>


----------



## zero11 (Jul 4, 2005)

Thanks my friend. You have explained very well. I think I understand well now. thanks a lot.


----------

