i am very, very very new to all of this.. seem to have got what i wanted for the most part.. i am putting a little weather widget on our business website... and have everthing working fine except sunset, sunrise...
i think because it has subcatagories..
how do i pull these??
see below:
<sunset>
<hour>20</hour>
<minute>48</minute>
</sunset>

<sunrise>
<hour>5</hour>
<minute>06</minute>
</sunrise>

im trying to call it using: $sun = "Dawn: $sunrise - Dusk: $sunset";

but it comes up just blank
if i just use $hour:$minute it defaults to the sunrise hour minute (must read bottom to top?)
im sure this is super simple. i am way over my head.. but hey.. so far it looks great!

thanks again!

pj

man, been at this all day.. and getting no where...

$file = "http://api.wunderground.com/api/conditions/forecast/astronomy/q/ZIP.xml";
$xml_parser = xml_parser_create();

     if (file_exists($file)){
            $data = implode('', file($file));
         } else {
            $fp = fopen($file,'r');
            while(!feof($fp)){
               $data = $data . fread($fp, 1024);
            }
            fclose($fp);
    }

xml_parse_into_struct( $xml_parser, $data, $vals, $index );
xml_parser_free( $xml_parser );

$t = true;

foreach ($vals as $key => $i) {

if ($vals[$key]['value'] != false) {

    if ($vals[$key]['tag'] != 'T') { 

        $temp_f = strtolower($vals[$key]['tag']);
        $$temp_f = $vals[$key]['value'];

    } elseif ($t == true) { 

        if ($vals[$key]['level'] == 3) $conditions = $vals[$key]['value']; 
        else { $wind_mph = $vals[$key]['value']; $t = false; break; }

    } 
}

}

i am able to call elements like this, $wind, $temp_f etc...
but there is sunrise

<sunrise>
    <hour>
     <minute>



 and <sunset>
             <hour>
             <minute>

i dont know how to call sunrise hour:minute

i dont want to put them in an array.. i fear they will change order... wheras now i can call them by the names they are given in the xml file

does this make any sense..
thanks
p

Could we see the full XML structure your using?

Also an array wouldnt change order unless you changed the order programatically.

Edited 4 Years Ago by Mike Askew: reword

<response>
<version>0.1</version>
−
<termsofService>
http://www.wunderground.com/weather/api/d/terms.html
</termsofService>
−
<features>
<feature>conditions</feature>
<feature>astronomy</feature>
</features>
−
<current_observation>
−
<image>
−
<url>
http://icons-ak.wxug.com/graphics/wu2/logo_130x80.png
</url>
<title>Weather Underground</title>
<link>http://www.wunderground.com</link>
</image>
<station_id>MTS546</station_id>
<observation_time>Last Updated on May 31, 6:44 PM CDT</observation_time>
<observation_time_rfc822>Thu, 31 May 2012 18:44:00 -0500</observation_time_rfc822>
<observation_epoch>1338507840</observation_epoch>
<local_time_rfc822>Thu, 31 May 2012 19:11:13 -0500</local_time_rfc822>
<local_epoch>1338509473</local_epoch>
<local_tz_short>CDT</local_tz_short>
<local_tz_long>America/Chicago</local_tz_long>
<local_tz_offset>-0500</local_tz_offset>
<weather>Clear</weather>
<temperature_string>57 F (13.9 C)</temperature_string>
<temp_f>57</temp_f>
<temp_c>13.9</temp_c>
<relative_humidity>33%</relative_humidity>
<wind_string>From the SW at 6 MPH Gusting to 13.0 MPH</wind_string>
<wind_dir>SW</wind_dir>
<wind_degrees>228</wind_degrees>
<wind_mph>6</wind_mph>
<wind_gust_mph>13.0</wind_gust_mph>
<wind_kph>9.7</wind_kph>
<wind_gust_kph>20.9</wind_gust_kph>
<pressure_mb>1017</pressure_mb>
<pressure_in>30.03</pressure_in>
<pressure_trend>0</pressure_trend>
<dewpoint_string>28 F (-2 C)</dewpoint_string>
<dewpoint_f>28</dewpoint_f>
<dewpoint_c>-2</dewpoint_c>
<heat_index_string>NA</heat_index_string>
<heat_index_f>NA</heat_index_f>
<heat_index_c>NA</heat_index_c>
<windchill_string>NA</windchill_string>
<windchill_f>NA</windchill_f>
<windchill_c>NA</windchill_c>
<feelslike_string>57 F (13.9 C)</feelslike_string>
<feelslike_f>57</feelslike_f>
<feelslike_c>13.9</feelslike_c>
<visibility_mi>10.0</visibility_mi>
<visibility_km>16.1</visibility_km>
<solarradiation>342</solarradiation>
<UV>2</UV>
<precip_1hr_string>0.00 in ( 0 mm)</precip_1hr_string>
<precip_1hr_in>0.00</precip_1hr_in>
<precip_1hr_metric> 0</precip_1hr_metric>
<precip_today_string>0.00 in (0 mm)</precip_today_string>
<precip_today_in>0.00</precip_today_in>
<precip_today_metric>0</precip_today_metric>
<icon>clear</icon>
<icon_url>http://icons-ak.wxug.com/i/c/k/clear.gif</icon_url>
<forecast_url>http://www.wunderground.com/US/MN/Hovland.html</forecast_url>
−
<history_url>
http://www.wunderground.com/history/airport/MTS546/2012/5/31/DailyHistory.html
</history_url>
−
<ob_url>
http://www.wunderground.com/cgi-bin/findweather/getForecast?query=47.950298,-89.779999
</ob_url>
</current_observation>
−
<moon_phase>
<percentIlluminated>83</percentIlluminated>
<ageOfMoon>11</ageOfMoon>
−
<current_time>
<hour>19</hour>
<minute>11</minute>
</current_time>
−
<sunset>
<hour>20</hour>
<minute>50</minute>
</sunset>
−
<sunrise>
<hour>5</hour>
<minute>05</minute>
</sunrise>
</moon_phase>
</response>

is the one i am using now.. but there is another one that i will be using with forecasts.. but i will run into the same problem... because the child tags are the same name for all the days it is the parent that is unique...

i have it cron-jobed every ten minuts.. and written to a text file in html, and i insert the txt file into my page...
you can see it in action here: http://naniboujou.com (bottom center)
i want to add sunrise and sunset, and then i will have it when you click it, it will give out a three day forecast
thank you for responding!

pj

my code is a messsss.. it is working, but i dont know what i am really doing... what i want, is a script that will read the xml file and write it to my txt file (i want it to write the current weather and condition to weather.txt), and the forecasted weather to weather_forecast.txt) i have a cron job set for every 10 mins.

so parse the data, write it to two files

for someone who knows what they are doing i imaging this would be very easy... i know close to nothing with php (i can cobble things together is about the extent of my php abilities.. if it isnt already clear_)

thanks!
pj

can anyone point me to a forum where i may get some help with this question?

anyone have any ideas.. trying to figure this out again.. a year later...

still clueless here... here is my code

<?php

// WEATHER.COM XML PARSER
// Version 1.4
// Copyright 2005 Nick Schaffner
// http://53x11.com


// $zipcode = 'zip';
// $partner = 'notused';
$wfile   = 'weather.html';      
// $icons    = 'images/weather';
// $email    = false;
// $mailto   = 'you@yourdomain.com';
$units   = 'english';
$clock  = 12;

$url     = 'http://api.wunderground.com/api/';  // URL to xoap.weather

//////// Parse XML

$file = "http://api.wunderground.com/api/myAPI/conditions/astronomy/q/zip.xml";

$xml_parser = xml_parser_create();

     if (file_exists($file)){
            $data = implode('', file($file));
         } else {
            $fp = fopen($file,'r');
            while(!feof($fp)){
               $data = $data . fread($fp, 1024);
            }
            fclose($fp);
    }

xml_parse_into_struct( $xml_parser, $data, $vals, $index );
xml_parser_free( $xml_parser );

$t = true;

foreach ($vals as $key => $i) {

if ($vals[$key]['value'] != false) {

    if ($vals[$key]['tag'] != 'T') { 

        $temp_f = strtolower($vals[$key]['tag']);
        $$temp_f = $vals[$key]['value'];

    } elseif ($t == true) { 

        if ($vals[$key]['level'] == 3) $conditions = $vals[$key]['value']; 
        else { $wind_mph = $vals[$key]['value']; $t = false; break; }

    } 
}

}


function output($conditions,$icon,$temp_f,$wind_mph,$sun,$weather,$date) { return "
//////// THIS CODE BELOW IS ALL I CARE ABOUT
<table bgcolor='white' class='shadow' border='1' RULES=NONE FRAME=BOX cellspacing='0' cellpadding='8'>
    <tr>
    <td style='padding: 0px 10px 0px 10px; valign='top' align='center'>$icon<br><font size='-2'>$weather</font></td>
    <td valign='top' align='left'><font size='-1'>Temp: $temp_f <br />Wind: $wind_mph mph </font></td>
    </tr>
</table>
"; }



$date = date("l F jS, Y @ g:ia", strtotime("- 1 hour"));
$temp = '&#176;F';
$wind_unit = 'mph';

if ($units == 'metric') { // Set Metric
    $tmp = round((($tmp - 32)/9) * 5);
    $flik = round((($flik - 32)/9) * 5);
    $temp_unit = '&#176;C';
    $s = round($s * 1.609);
    $wind_unit = 'kmh';
}


$sun = "Dawn: $sunrise  - Dusk: $sunset";

if ($tmp != $flik) $temp_f = "<b>$tmp$temp_f</b> <i>(feels like $feelslike_f)</i>"; 
else $temp = "$feelslike_f&#176; F";

if ($s == 'calm') $wind_mph = "<b>Calm</b>";
else $wind = "<b>$wind_dir</b> @ <b>". $s . "</b> ";

$icon = "<img border='0' alt='$weather' height='32' width='32' src='$icon_url' />";


$output = trim(output($conditions,$icon,$temp,$wind_mph,$sun,$weather,$date));
$output = "$output<!-- Generated on $date -->";


//////// Write to file

$fd = fopen ($wfile , "w"); 
fwrite ($fd , $output);
fclose($fd);

//////// Email and Cron Job output

$replace = array('<','>','/','b','i');
$temp = str_replace($replace, '', $temp);
$wind = str_replace($replace, '', $wind);
$temp = str_replace('&#176;', '°', $temp);

$message = "Weather report for $zipcode\n\n\nTemp: $feelslike_f\nF Wind: $wind_string\n\n$sunset \n\nGenerated on $date ";

echo $message;

if ($email) mail($mailto,"Parse Results for $date",$message, "From: $mailto");

?> 
This article has been dead for over six months. Start a new discussion instead.