minitutorials main logo the place to learn online

Sponsor This Site!
Sponsorship Details - miniTutorials is taking enquiries into various Sponsorship opportunities. Available NOW! Read More on our Sponsorship page.
This Page Sponsored by :
Proudly Sponsored by Your Company name here!
miniTutorials Sponsorship
Donate!
Consider donating to keep this site alive and up to date. Click on the Donate button to go straight to PayPal site and make a donation of an amount you choose.


More information on where the money goes on the Donations page.
Discussion Forums
visit the mini tutorial forums
For expert help and advice on any of our Tutorials or anything else .... visit the forums and ask away!!
Google Ads
My Profile

Details about the owner of this site can be found at :-

ohloh profile
View Gavin McDonald's profile on LinkedIn
Powered by a
UK2 Dedicated Server

Dynamic Content

What is Dynamic Content?

a different way to add dynamic content

There are many ways to add dynamic content to a website, utilising different server side technologies and scripts. What do I mean by 'dynamic content' is the first thing I should clear up. Well, content that changes automatically once it has been set up - change the source of the content and the content updates on your web pages automatically (there are other variations but this is the general gist).

I am going to concentrate on a different technique, using the weather service as an example, you can then use your imagination (and some links at the end of the tutorial) to create your own dynamic content using the same technique.

 

What you will need for this tutorial :

Note: No knowledge of FTP commands is required to follow this tutorial as all the code is provided, however in order to adapt the scripts to your own needs a little understanding will be helpful. Hopefully this tutorial will just 'show you the way'. Any help in adapting the scripts to your needs is just an email away or of course you can ask in our forums for any help needed. No knowledge of PHP is required in order to complete this tutorial but for adapting the tutorial to your own needs some PHP knowledge will be useful.

Top

Steps to Success:

Step 1 - Find the data source.
Step 2 - Download the data source from its web (ftp) location.
Step 3 - Upload the data source to your website space.
Step 4 - Parse the data.
Step 5 - Display the data on your web page(s)

All of the above steps once applied will do their job automatically, some via the use of command line scripts - good old batch files (.bat) still have their uses.!!

 

Step 1 - find the data source

Luckily, for this example I know where the data source is, for your own use of course you will need to find your own by searching sites that will let you automatically download files from an ftp location.

We are going to be getting our data from the Bureau of Meteorology in Australia, you might like to , after testing this example, find the weather service for your Country, most of them will offer this kind of service.

If you take a look around the site you will find an abundance of information to be had, temperatures, weather warnings, forecasts etc. I decided on the forecast for Western Australia. The page that holds the information we want is at http://www.bom.gov.au/cgi-bin/wrap_fwo.pl?IDW12000.txt . Have a look at the information on this page and come back. There is a fair amount of text on there and we don't really need it all, we will strip out what we want later, first of all we need to use the ftp location of where this file is kept. We know the name - IDW12000.txt , the important thing here is that the BOM keep this same filename and update it every day with the latest information. All we do is check the file every few hours, decide if it has been updated, then download it to our computer and re-upload it to our server. (Note: If you are running your own web server you can place the scripts on the server and download direct to your web space without the need to re-upload, this tutorial assumes that the majority do not have their own server)

On surfing the site, I came across the ftp details I needed in order to get the file I needed. Looking at http://www.bom.gov.au/other/Ftp.shtml , about half way doen the page in the 'Anonymous FTP' section is the info we need, a line that says :

All current forecasts and warnings are at: ftp2.bom.gov.au/anon/gen/fwo

So we click on that link , and it confirms to us that the '/anon/gen/fwo' ftp directory holds a lot of regularly updated weather files - including the IDW12000.txt file that we are really interested in. But we don't download it through the web browser, we just make a note of the files location ready for one of our batch files later on.

Ok, so I wont nanny you any more, lets get on with the action!!

Top

step 2 - download the data source

As briefly mentioned earlier, you can, if you have your own web server, download directly into your web space at the appropriate point. I am going to show here the way to do it via your home computer, downloading to there and then re-uploading to the webspace provided by your ISP.

We will need to create two files for the fetching of the 'IDW12000.txt' file. The first is a batch file (getweatherreport.bat) which contains the ftp commands to fetch the file. The second is a text file (getweatherreport.txt) that contains the details of the file to fetch. I'm sure there might be a simpler way to do it, but found this to be the only reliable method for me.

getweatherreport.bat

ftp -n -s:getweatherreport.txt ftp2.bom.gov.au putweatherreport.bat

The first line tells the computer to open up an ftp connection , using the -n switch to prevent automatic login, using the -s switch to specify a filename that contains further commands - in this case getweatherreport.txt, finally the location of the connection we want - ftp2.bom.gov.au.

The second line tells the computer to then run the next batch file which will put the newly received file into our webspace. We will look at that in the next section.

getweatherreport.txt

user ftp username@isp.net get /anon/gen/fwo/IDW12000.txt disconnect bye

The ftp commands we send to the ftp2.bom.gov.au ftp server to fetch the file. The bom.gov.au website tells us what login information it would like from us to accept the connection for file transfer, in this case we use 'user' and no name for anonymous login and specify an email address for a password. The next line we use the ftp command 'get' to well erm.. get the file! the rest of that line specifys the location on the server of the file and the filename (which we found earlier by browsing the site.)

Finally in this file we send a gratious command to disconnect and say bye to the ftp server.

Create the two files with the contents as above and save in a suitable location on your computer, suitable would be in the local version of your website in the root directory. This is so that the 'IDW12000.txt' file gets saved into your web site structure in the correct place (without having to specify it).

Now, in Windows create a 'Scheduled Task' to run the 'getweatherreport.bat' file at say 4 hour intervals. Create a cron job or whatever you do in Linux to do the same. Obviously, this is where the 'semi-permanent internet connection' requirement comes in - your computer needs to be connected to the internet during the scheduled times for the transfers to be successful. I am not going to explain how to create a Scheduled Task or cron job here, but if you do need help in this area , please use the forums and ask for help, I will gladly explain it there - you don't need to be a member to ask questions, but it is free so why not :)

Top

step 3 - upload the data source (to your webspace)

Ok, so you get the gist of things now. What we do now is create another pair of files to put the 'IDW12000.txt' from our local computer to your ISP webspace.

putweatherreport.bat

ftp -n -s:putweatherreport.txt ftp.yourwebaddress.com

Just the one line in this batch file, as we don't need to run any other program afterwards. Same deal, we make an ftp connection and specify a file that contains the ftp commands for the session. Finally on this line we specify the ftp location we want to send the file to - your website space, so in order for this to work you need to substitute 'ftp.yourwebaddress.com' with the ftp details of your website.

putweatherreport.txt

user yourusername yourpassword put IDW12000.txt disconnect bye

The first line you must specify your username and password that you use to connect to your website. The second line we 'put' the file we have just received into our website space - with no path specified it will be placed in the root of your website. Then we again gracefully disconnect from your server.

Top

step 4 - parse the data

Ok, so having looked at the contents of 'IDW12000.txt' earlier, we can assume that we do not want to display its entire contents word for word. What we do is use a little bit of PHP to look through the contents of the file (now on the web server) and pick out the bits we do want.

Now working on our 'index.php' file - or whichever file on your site you want to display the weather data, we add this PHP code in a suitable place in your web page. The code below contains functions so you can place them anywhere within the main body of your page, I would suggest just below where you want the data displayed or near the end of your code.

<?php function displayPerthDetails() // The file name. // NOTE: this could be the FTP URL instead of the filename but slower. // To get report directly from BOM replace next line with $file = "ftp://ftp2.bom.gov.au/anon/gen/fwo/IDW12000.txt"; $file = "IDW12000.txt"; // Open the file. $file_pointer = fopen( $file, "r" ); // Lets throw away the stuff we dont want. $line = ""; $body = ""; while( !feof( $file_pointer )) { // Read a line in. $line = fgets( $file_pointer, 4096 ); // If it contains the string "PERTH DETAILS" for example. if ( stristr( $line, "FORECAST TEMPERATURES" ) ) { // then move on to the next stage. break; } } // lets build up the body. while( !feof( $file_pointer )&& ( (stristr($line, "\n")))) { // Get a line $line = fgets( $file_pointer, 4096 ); // Exchange all newlines with <br />'s $line = str_replace( "\n", "<br />", $line ); // Remove carriage returns
$line = str_replace( "\r", "", $line ); // Add it to the body. $body = $body.$line; } // close the file fclose( $file_pointer ); // return the body for printing. return $body; } ?> <?php function displayIssuedAt() { // The file name. // NOTE: this could be the FTP URL instead of the filename but slower. // To get report directly from BOM replace next line with $file = "ftp://ftp2.bom.gov.au/anon/gen/fwo/IDW12000.txt"; $file = "IDW12000.txt"; // Open the file. $file_pointer = fopen( $file, "r" ); // Lets throw away the stuff we dont want. $line = ""; $body = ""; while( !feof( $file_pointer )) { // Read a line in. $line = fgets( $file_pointer, 4096 ); // If it contains the string "MEDIUM RANGE" if ( stristr( $line, "FORECASTS FOR WESTERN AUSTRALIA" ) ) { // then move on to the next stage. break; } } // lets build up the body, stopping when the line finished while( !feof( $file_pointer ) && ( (stristr($line, "\n")))) { // Get a line $line = fgets( $file_pointer, 4096 ); // Exchange all newlines with <br />'s $line = str_replace( "\n", "<br />", $line ); // Remove carriage returns $line = str_replace( "\r", "", $line ); // Add it to the body. //$line = str_replace( " ", "&nbsp; ", $line ); $body = $body.$line; } // close the file fclose( $file_pointer ); // return the body for printing. return $body; } ?>

Wow, there seems to be quite a bit in there! I have left in comments to explain what each section does, a PHP tutorial this is not so I wont explain the code any further suffice to say it works and you shouldn't need to change a thing. Any further explanations or help with adapting the code for your needs can be obtained from the forums here at ITgazette.

step 5 - display the data

Last we call the functions in the code above and place them in the position in the web page where we want the weather displayed.

<div class="aligncenter">Weather for <?=displayPerthDetails();?> This information was <?=displayIssuedAt();?> by the <a href="http://www.bom.gov.au">Bureau of Meteorology</a></div>

Something like the above should do it. I think we are finally finished!

Top

 

summary

You can check out a working version of the above tutorial on the main index page of minitutorials.com - near the bottom will be the weather details for Perth. This changes every 4 hours assuming the file has been updated by the BOM during that time - and I don't have to do a damn thing !!!. Whenever it doesn't work is when my internet connection or ftp server is down, but thats my problem :)

Top

Want to know more?

You can do the above tutorial in a different way. There are comments within the PHP code that hints at the fact you could specify an ftp location instead of a filename. Doing it that way means you will NOT need to do any of steps 2 or 3 - in other words no batch files, no scheduled tasks, nothing, just specify the location of the file directly in the PHP code above. This means however that every time your web page gets served, off it goes to the Bureau of Meteorology to fetch the file and put it on your (ISPs) web server. I found this wasteful connnections and slowed the page loading time down quite a lot. I also thought it fair on the BOM servers if I only checked for it every 4 hours.

You can also go further with the weather reports, most weather services will let you download icons of Sun, Cloud, etc etc and you can alter the PHP script to display them depending on the weather.!

Let your imagination run riot with the above tutorial, it doesn't have to be the weather, it can be cricket or football results, data from other web sites or services - you just have to look and see what's out there.

Here are a few good links to some useful information that might help.

http://www.computerguru.net/tech/netw71.htm
http://www.wown.info/j_helmig/ftpcomm.htm
http://www.bom.gov.au/other/Ftp.shtml

irca content rating labelValid CSSValid XHTML 1.0 Strict[Valid RSS] About Us | Site Map | Privacy Policy | Contact Us | ©2003 - 2008 miniTutorials.com link to minitutorials xml feedLink to RSS Feed

Site Design by 16 degrees complete web solutions.