Jump to content

Pulling weather info from xml


kevin87
Go to solution Solved by boompa,

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
Share on other sites

  • Solution

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;
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.