Jump to content

Pulling weather info from xml


kevin87

Recommended Posts

Hi everyone,

 

I am having a little trouble understanding what is going on. Below is the code I am working with. The data is not printing on the screen. What the heck am I doing wrong? Any input is appreciated.

 

Thanks,

Kevin

<?php
$xmlMetar=simplexml_load_file("http://aviationweather.gov/adds/dataserver_current/httpparam?dataSource=metars&requestType=retrieve&format=xml&stationString=KARR&hoursBeforeNow=1");
$metarKARR=$xmlMetar->METAR->raw_text;
echo $metarKARR;
?>
Link to comment
https://forums.phpfreaks.com/topic/285675-pulling-weather-info-from-xml/
Share on other sites

If you dump the value of $xmlMetar with print_r($xmlMetar), you get this:

 

 

SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [version] => 1.2
        )

    [request_index] => 5490740
    [data_source] => SimpleXMLElement Object
        (
            [@attributes] => Array
                (
                    [name] => metars
                )

        )

    [request] => SimpleXMLElement Object
        (
            [@attributes] => Array
                (
                    [type] => retrieve
                )

        )

    [errors] => SimpleXMLElement Object
        (
        )

    [warnings] => SimpleXMLElement Object
        (
        )

    [time_taken_ms] => 2
    [data] => SimpleXMLElement Object
        (
            [@attributes] => Array
                (
                    [num_results] => 1
                )

            [METAR] => SimpleXMLElement Object
                (
                    [raw_text] => KARR 251852Z 31023G33KT 10SM CLR M09/M19 A2972 RMK AO2 PK WND 30033/1846 SLP074 T10941189
                    [station_id] => KARR
                    [observation_time] => 2014-01-25T18:52:00Z
                    [latitude] => 41.77
                    [longitude] => -88.48
                    [temp_c] => -9.4
                    [dewpoint_c] => -18.9
                    [wind_dir_degrees] => 310
                    [wind_speed_kt] => 23
                    [wind_gust_kt] => 33
                    [visibility_statute_mi] => 10.0
                    [altim_in_hg] => 29.719488
                    [sea_level_pressure_mb] => 1007.4
                    [quality_control_flags] => SimpleXMLElement Object
                        (
                            [auto_station] => TRUE
                        )

                    [sky_condition] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [sky_cover] => CLR
                                )

                        )

                    [flight_category] => VFR
                    [metar_type] => METAR
                    [elevation_m] => 215.0
                )

        )

)

 

As you can see, the METAR element is within a data element, so you must access that before METAR:

 

 

$metarKARR=$xmlMetar->data->METAR->raw_text;

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.