Recommended sites
Web Hosting Reviews

Get actual weather information

In this tutorial I will show you how you can retrive actual weather information from Yahoo Weather and display this information on your own site.

 

Get actual weather information

To get the actual weather conditions first you need to find a good source where this information is present and accurate. A good choice is the Yahoo weather where you can find the actual weather information for more thousand cities of the world. You can find it here: weather.yahoo.com

 

Step 1.

 

Before any coding first find the correct page with the requested information. The best is to find an RSS feed as it is easier to work with a smaller XML file. On Yahoo you can do it as follows:

  1. Visit http://weather.yahoo.com
  2. Select the continent in the Browse for U.S. and International Forecasts section
  3. Select the requested country and city
  4. Select the unit to use F or C (Center above the weather image)
  5. As next step select the RSS icon near top right
  6. Copy this link, as we will use it in our script. It should look like similar to this: http://xml.weather.yahoo.com/forecastrss?p=FRXX0076&u=c

 

Step 2.

 

Now we have the source information so it's time to process it. We can devide the task into 3 subtasks as follows:

  1. Open and read the complete RSS link and content
  2. Analyze the content and get the important data
  3. Display weather information

To do this we will create a function getWeatherRSS to read the RSS content. This function gets the weather URL as parameter and uses to normal fopen and fread functions to get the complete RSS code. AT the end it returns with the RSS feed as a normal string.

 

The code is the following:

 

function getWeatherRSS($weatherLink){

if (
$fp = fopen($weatherLink, 'r')) {
$content = '';

while (
$line = fread($fp, 1024)) {
$content .= $line;
}
}

return
$content;
}
?>

 

Step 3.

 

As we have the RSS string it's time to process it and find the necessary information inside the string. What we need anyhow? We want to know the actual temperature, the temperature unit C or F, and the actual city name. All of them are included inside the RSS string we just need to find and fetch them.

We well do this job in a function let's call it processWeather. To make the usage of our system more easy we will call the above implemented getWeatherRSS function as the first step.

Next we initialize some variables to store temperature, temperature unit and city name.

After it we make a small check if the RSS feed is long enough. If the length of the string is smaller than 100 characters then something is wrong.

So the skeleton of the function looks like this:

 

function processWeather($wurl){

$wrss = getWeatherRSS($wurl);
$temp = '-';
$tempu = '';
$city = '';

if (
strlen($wrss)>100){

}

}
?>

 

Now we need to make some string manipulation to get weather condition from the main string.

First try to get the temperature unit from the string. If you look at the source code of the RSS you will find the unit in the yweather:units section. Fortunately there is only one such string so you can make a substring search on the original string to find yweather:units temperature=" . As strpos returns with the starting position of the string and we need the last position so we need to update the result with the length of the substring. To get the end position we just need to find the next double quote and then we can get the temperature unit by getting the substring between the start and end positions.

The code looks like this:

 

// Get temperature unit C or F
$spos = strpos($wrss,'yweather:units temperature="')
+
strlen('yweather:units temperature="');
$epos = strpos($wrss,'"',$spos);
if (
$epos>$spos){
$tempu = substr($wrss,$spos,$epos-$spos);
}
?>

You need to repeat this step to get the temperature and the city name as well. After we have all 3 parameter we can build a new string - formatted as you want - and return it.

 

Step 4.

 

The last step is quite easy. You only need to create a small php page and call processWeather and display the return value.

That's it.

You can download a complete weather script from the download section.

[newpage=Complete code]

<?php

function getWeatherRSS($weatherLink){
   
   if (
$fp fopen($weatherLink'r')) {
      
$content '';
        
      while (
$line fread($fp1024)) {
         
$content .= $line;
      }
   }

   return 
$content;  
}

function 
processWeather($wurl){
    
    
$wrss getWeatherRSS($wurl);
    
$temp  '-';
    
$tempu '';
    
$city  '';
    
    if (
strlen($wrss)>100){
        
// Get temperature unit C or F
        
$spos strpos($wrss,'yweather:units temperature="')
              + 
strlen('yweather:units temperature="');
        
$epos strpos($wrss,'"',$spos);
        if (
$epos>$spos){
            
$tempu substr($wrss,$spos,$epos-$spos);
        } 

        
$spos strpos($wrss,'yweather:wind chill="')
              + 
strlen('yweather:wind chill="');
        
$epos strpos($wrss,'"',$spos);
        if (
$epos>$spos){
            
$temp += substr($wrss,$spos,$epos-$spos);
        } else {
            
$temp '-';
        }
        
        
// Get city name
        
$spos strpos($wrss,'yweather:location city="')
              + 
strlen('yweather:location city="');
        
$epos strpos($wrss,'"',$spos);
        if (
$epos>$spos){
            
$city substr($wrss,$spos,$epos-$spos);
        } 

    }
    
    return 
$city.' &nbsp;'.$temp.' &deg;'.$tempu;
    
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
<html>
<head>
   <title>Micro Weather</title>
   <link href="style/style.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <div id="main">
      <div id="caption">CURRENT WEATHER</div>
      <div id="icon2">&nbsp;</div>
      <div id="result"><?php echo processWeather('http://xml.weather.yahoo.com/forecastrss?p=USTX0327&u=c'); ?>
      </div>
      <div id="source">Micro Weather 1.0</div>
    </div>
</body>   

Sponsored links